zoukankan      html  css  js  c++  java
  • IOS练习一

    练习,按下图,然后计算在Lable里面的字符串的长度,最后在下面输出字符串长度

    3.jpg

    首先在MainMenu.xib绘制框

    1.jpg

    然后要求当运行的界面初始为如下:

    2.jpg

    嗯。开始代码吧

    首先.h 文件

    //
    //  countCharacterAppDelegate.h
    //  countCharacter
    //
    //  Created by mutou on 14-11-28.
    //  Copyright 2014年 __MyCompanyName__. All rights reserved.
    //
    
    #import <Cocoa/Cocoa.h>
    
    @interface countCharacterAppDelegate : NSObject <NSApplicationDelegate> {
    @private
        NSWindow *window;
        NSTextField *inTextField;
        NSTextFieldCell *resultTextField;
    }
    @property (assign) IBOutlet NSTextField *inTextField;  //输入的文本框
    @property (assign) IBOutlet NSTextFieldCell *resultTextField;  //计算结果的输出
    
    @property (assign) IBOutlet NSWindow *window;         
    - (IBAction)actCount:(id)sender;           //点击按钮
    
    @end
    

     这个.h 文件,用鼠标拖,点就出来了。(额,鼠标)。(具体操作,在《苹果开发之Cocoa编程 第4版》的5.2-5.3 以及 《objective-c基础教程 第2版》的15章AppKit 有讲解,或网上教程)

    然后的主要实现,在于.m文件

    //
    //  countCharacterAppDelegate.m
    //  countCharacter
    //
    //  Created by mutou on 14-11-28.
    //  Copyright 2014年 __MyCompanyName__. All rights reserved.
    //
    
    #import "countCharacterAppDelegate.h"
    
    @implementation countCharacterAppDelegate
    
    @synthesize inTextField;
    @synthesize resultTextField;
    @synthesize window;
    
    - (void) awakeFromNib
    {   
            [inTextField setStringValue:@"Enter text here"];         //运行时,输入的text里面显示Enter text here
            [resultTextField setStringValue:@"???"];                 //而在结果里面的lable显示???,也就是实现上面最后一个图
    }
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
    }
    
    - (IBAction)actCount:(id)sender {
        NSString *string = [inTextField stringValue];               //获取text里面的字符
        int strNum = [string length];                               //计算出字符串长度
        NSLog(@"(%@) character is :%d",string,strNum);              //在下面的输出栏看,算是Log吧
        
        //额,之前卡了下在下面的这句语句。+(NSString *) stringWithFormat:(NSString *),...; 主要是这个方法没有用对。
        NSString *resultString =  [NSString stringWithFormat:@"This is only a test's has %d characters",strNum];
        [resultTextField setStringValue:resultString];              //把上面的那句话赋给显示的lable就好了
    }
    @end
    

     记录笔记,下次不记得,回来看看

  • 相关阅读:
    uniapp IOS使用uni.getLocation获取不到具体城市名字
    uniapp 打开[ios/安卓]GPS定位权限
    for循环中利用计时器使用let和var
    uniapp 调起底部输入框textarea聊天页面被键盘顶起
    父子组件传布尔类型,发现有问题一直传字符串
    Kafka学习-基础知识
    剑指offer-删除链表中重复的节点
    LEACH协议原文详解
    主流机器学习框架
    算法工程师-职位描述
  • 原文地址:https://www.cnblogs.com/LoveDan/p/4127784.html
Copyright © 2011-2022 走看看