ios开发学习笔记(三)

    顺着上次的进度,今天开始在Xcode的新建一个小的ios应用程序,步骤如此网站所示,它教会我们:

  • 如何应用xcode新建一个基于单视图模板的工程,并且编译和运行它。
  • 介绍工程中的基本的一些文件,如main.m源文件,Info.plist文件和storyboard文件,并介绍如何启动app
  • 介绍什么是MVC设计模式,并介绍在app开发中mvc模式是如何应用的
  • 解释启动时的app的背景为何是白色,以及如何修改它
  • 如何将view controller中的V和扩展类文件(ViewController.m)中的C联系起来,就是更新这个类文件来支持outlets和action,不仅可以用control+dragging来生成代码,也可以自己写代码,最后连接起来即可。

    ViewController中要学之事:

  •     如何添加UI元素并且正确的显示他们
  •     如何配置textview元素
  •     如何给Button添加action
  •     如何给textField添加outlet
  •     如何给label添加outlet
  •     如何打开view controller 的链接指示器
  •     如何设置textField的delegate
  •     如何给textField添加hint

    类扩展文件中要学之事:

    Viewcontroller.h 代码:

#import <UIKit/UIKit.h>
 
@interface HelloWorldViewController : UIViewController <UITextFieldDelegate>
 
@property (copy, nonatomic) NSString *userName;
 
@end

     Viewcontroller.m代码:

    

#import "HelloWorldViewController.h"
 
@interface HelloWorldViewController ()
 
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UILabel *label;
 
- (IBAction)changeGreeting:(id)sender;
 
@end
 
@implementation HelloWorldViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textField.delegate = self;
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
 
- (IBAction)changeGreeting:(id)sender {
 
    self.userName = self.textField.text;
 
    NSString *nameString = self.userName;
    if ([nameString length] == 0) {
        nameString = @"World";
    }
    NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
    self.label.text = greeting;
}
 
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
 
    if (theTextField == self.textField) {
        [theTextField resignFirstResponder];
    }
    return YES;
}
 
@end

     最后运行即可看到效果,附件为工程代码。

猜你喜欢

转载自frand-feng.iteye.com/blog/1876564