• 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
    

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

  • 相关阅读:
    2018前端越来越流行的的技术
    程序员怎么写出一份漂亮的简历
    程序员怎么写出一份漂亮的简历
    程序员如何利用空闲时间挣零花钱
    扫雷(mine)
    一道数论好题(math)
    消失的数字(number)
    1380 没有上司的舞会 40分
    2596 售货员的难题 水深搜
    JavaEE Tutorials (17)
  • 原文地址:https://www.cnblogs.com/LoveDan/p/4127784.html
Copyright © 2020-2023  润新知