美文网首页
UI总结-UIViewController

UI总结-UIViewController

作者: Dear丶Musk | 来源:发表于2016-05-03 21:49 被阅读42次

                               UI总结-UIViewController

    在AppDelegate.m文件中:

    #import "AppDelegate.h"

    #import "RootViewController.h"

    #import "AppDelegate.h"

    #import "RootViewController.h"

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    [self.window makeKeyAndVisible];

    [_window release];

    //就iOS开发来说,UIViewController就最核心的类型之一。而iOS的整个UI开发的核心思想也是MVC的架构,从UIViewController的命名就可以看出它在MVC中所扮演的角色,那就是Controller啦。

    //创建一个视图控制器

    RootViewController *vc = [[RootViewController alloc]init];

    //给window指定一个根试图控制器

    self.window.rootViewController = vc;

    [vc release];

    return YES;

    }

    在RootViewController.m文件中:

    #import "RootViewController.h"

    #import "CWCView.h"

    @interface RootViewController ()

    @end

    @implementation RootViewController

    // ViewController 的初始化方法

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

    //这个方法即使不调用,在使用的过程中,系统也会自己调用这个方法

    //一般在这个方法里面写容器的初始化,比如数组,字典等.

    NSLog(@"我被使用了");

    }

    return self;

    }

    -(void)loadView{

    //当view需要被展示而它却是nil时,viewController会调用该方法,如果代码构建View的话需要重写此方法。

    [super loadView];

    }

    //执行完loadView后继续执行viewDidLoad,loadView时还没有view,而viewDidLoad时view已经创建好了。

    - (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    [self creatView];

    NSLog(@"试图加载完成");

    }

    -(void)creatView{

    UIButton *button1 =[UIButton buttonWithType:UIButtonTypeCustom];

    button1 .frame = CGRectMake(100, 100, 100, 100);

    button1.backgroundColor = [UIColor blueColor];

    [button1 setTitle:@"确定" forState:UIControlStateNormal];

    button1.layer.borderWidth = 1;

    button1.layer.cornerRadius = 5;

    [self.view addSubview:button1];

    [button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    CWCView *myView = [[CWCView alloc]initWithFrame:CGRectMake(0, 50, 100, 100)];

    [self.view addSubview:myView];

    [myView release];

    myView.myLabel.text = @"姓名";

    myView.myTextField.placeholder = @"请输入姓名";

    }

    -(void)click:(UIButton *)button{

    NSLog(@"11111");

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"碉堡了" message:@"是吗" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"退出", nil];

    alert.alertViewStyle = 3;

    [alert show];

    [alert release];

    }

    //根据被点击按钮的索引处理点击事件

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    UITextField *text = [alertView textFieldAtIndex:0];

    NSLog(@"%@", text.text);

    }

    写了一个继承UIView的类, .h文件:

    #import<UIKit/UIKit.h>

    @interface CWCView : UIView

    @property(nonatomic, retain)UILabel *myLabel;

    @property(nonatomic, retain)UITextField *myTextField;

    @end

    .m文件:

    #import "CWCView.h"

    @interface CWCView ()

    @end

    @implementation CWCView

    -(id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

    //在创建view的过程中,完成label和textfield的创建

    [self creatView];

    }

    return self;

    }

    -(void)creatView{

    self.myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];

    self.myLabel.layer.borderWidth = 2;

    self.myLabel.layer.cornerRadius = 5;

    self.myLabel.textAlignment = 1;

    //self本身就是一个试图,所以,直接把空间添加到self上即可

    [self addSubview:self.myLabel];

    [_myLabel release];

    self.myTextField = [[UITextField alloc]initWithFrame:CGRectMake(150, 0, 150, 30)];

    self.myTextField.layer.borderWidth = 2;

    self.myTextField.layer.cornerRadius = 5;

    [self addSubview:self.myTextField];

    [_myTextField release];

    self.myTextField.clearButtonMode = UITextFieldViewModeAlways;

    self.myTextField .delegate = self;

    }

    -(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    return YES;

    }

    -(void)dealloc{

    [_myLabel release];

    [_myTextField release];

    [super dealloc];

    }

    相关文章

      网友评论

          本文标题:UI总结-UIViewController

          本文链接:https://www.haomeiwen.com/subject/mflrrttx.html