? ? ? ? ? ? ? ? ? ? ? ? ? ?UI總結(jié)-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開發(fā)來說,UIViewController就最核心的類型之一游昼。而iOS的整個(gè)UI開發(fā)的核心思想也是MVC的架構(gòu)免绿,從UIViewController的命名就可以看出它在MVC中所扮演的角色同蜻,那就是Controller啦终吼。
//創(chuàng)建一個(gè)視圖控制器
RootViewController *vc = [[RootViewController alloc]init];
//給window指定一個(gè)根試圖控制器
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) {
//這個(gè)方法即使不調(diào)用,在使用的過程中,系統(tǒng)也會自己調(diào)用這個(gè)方法
//一般在這個(gè)方法里面寫容器的初始化,比如數(shù)組,字典等.
NSLog(@"我被使用了");
}
return self;
}
-(void)loadView{
//當(dāng)view需要被展示而它卻是nil時(shí)输虱,viewController會調(diào)用該方法摘能,如果代碼構(gòu)建View的話需要重寫此方法站刑。
[super loadView];
}
//執(zhí)行完loadView后繼續(xù)執(zhí)行viewDidLoad瘸彤,loadView時(shí)還沒有view,而viewDidLoad時(shí)view已經(jīng)創(chuàng)建好了笛钝。
- (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];
}
//根據(jù)被點(diǎn)擊按鈕的索引處理點(diǎn)擊事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
UITextField *text = [alertView textFieldAtIndex:0];
NSLog(@"%@", text.text);
}
寫了一個(gè)繼承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) {
//在創(chuàng)建view的過程中,完成label和textfield的創(chuàng)建
[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本身就是一個(gè)試圖,所以,直接把空間添加到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];
}