?VC中 自帶一個view 用于鋪設視圖 默認顏色為透明
self.view.backgroundColor = [UIColor cyanColor];
BUTTON
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor yellowColor];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIImageView?
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
imgView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imgView];
[imgView release];
添加圖片
相對路徑 修改之后仍然可以正常顯示
絕對路徑 如果文件位置修改 就找不到了
?imgView.image = [UIImage imageNamed:@"color"];
收獲路徑(動態變化的絕對路徑)
?參數1: 文件名
?參數2: 文件后綴
NSString *path = [[NSBundle mainBundle] pathForResource:@"color" ofType:@"png"];
imgView.image = [UIImage imageWithContentsOfFile:path];
?圓角
imgView.layer.cornerRadius = imgView.frame.size.width / 2;
?根據邊界把多余部分切掉
imgView.clipsToBounds = YES;
用戶名
LTView *user = [[LTView alloc] initWithFrame:CGRectMake(0, 250, 375, 60)];
user.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:user];
[user release];
user.label.text = @"用戶名";
頁面跳轉(模態)
- (void)go
{
NSLog(@"點點點");
TwoViewController *twoVC = [[TwoViewController alloc] init];
[self presentViewController:twoVC animated:YES completion:^{
}];
}
模態返回
- (void)back
{
NSLog(@"返回");
[self dismissViewControllerAnimated:YES completion:^{
}];
}
導航
導航欄設置: controller(欄)/item(欄上的元素)
導航欄顯示/隱藏
self.navigationController.navigationBarHidden = NO;
或 self.navigationController.navigationBar.hidden = YES;
?欄樣式
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
?半透明效果
?開始效果時 屏幕左上角為坐標原點
?關閉時 導航欄的左下角為坐標原點
self.navigationController.navigationBar.translucent = YES;
?創建view(0,0,100,100)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
[view release];
?欄背景顏色
self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
?欄顏色
self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
?欄標題
self.title = @"這是一個標題";
self.navigationItem.title = @"這是一個猴賽雷的標題";
分段控制器
UISegmentedControl *seg = [[[UISegmentedControl alloc] initWithItems:@[@"消息", @"電話"]] autorelease];
seg.frame = CGRectMake(0, 0, 100, 30);
欄標題視圖
self.navigationItem.titleView = seg;
欄左側按鈕
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(left:)] autorelease];
欄右側按鈕
系統按鈕樣式
UIBarButtonItem *b1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(right1)] autorelease];
自定義按鈕圖片
UIBarButtonItem *b2 = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"dianzan"] style:UIBarButtonItemStylePlain target:self action:@selector(right2)] autorelease];
self.navigationItem.rightBarButtonItems = @[b1, b2];
修改導航欄上內容的顏色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
跳轉頁面
寫一個button
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(200, 200, 100, 100);
btn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:btn];
調用goTwo方法
[btn addTarget:self action:@selector(goTwo) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 跳轉頁面
- (void)goTwo
{
?1.獲取第二頁對象
TwoViewController *twoVC = [[TwoViewController alloc] init];
2.跳轉(由導航控制器 從當前push到第二頁)
[self.navigationController pushViewController:twoVC animated:YES];
3.內存管理
[twoVC release];
}
頁面返回
- (void)back
{
NSLog(@"返回");
?由導航控制器 控制當前VC返回上一頁
[self.navigationController popViewControllerAnimated:YES];
}
頁面間傳值
協議4.簽協議
@interface RootViewController ()<PassDelegate>
@property (nonatomic, retain) UITextField *tf1;
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"首頁";
self.tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
_tf1.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_tf1];
[_tf1 release];
UIButton *go = [UIButton buttonWithType:UIButtonTypeSystem];
go.frame = CGRectMake(100, 200, 200, 40);
[go setTitle:@"GO" forState:UIControlStateNormal];
go.backgroundColor = [UIColor redColor];
[go addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:go];
調用的方法
- (void)go
{
TwoViewController *twoVC = [[TwoViewController alloc] init];
屬性2: 在push頁面之前傳值(創建對象之后 push之前)
twoVC.string = self.f1.text;
協議5: 設置代理人
為了保證 設置代理人的對象和push的對象是同一個 在創建對象之后 push之前 設置delegate
twoVC.delegate = self;
[self.navigationController pushViewController:twoVC animated:YES];
[twoVC release];
}
協議6: 實現協議方法
- (void)passValue:(NSString *)string
{
把收到的string 賦值給輸入框
self.tf1.text = string;
}
TwoViewController.h中
協議1: 聲明協議(定義一個帶參數的方法)
@protocol PassDelegate<NSObject>
@optional
@required(默認)
- (void)passValue:(NSString *)string;
@end
@interface TwoViewController : UIViewController
屬性1: 在第二頁聲明一個屬性 用來保存數據
@property (nonatomic, copy) NSString *string;
協議2: 定義代理人屬性@property (nonatomic, assign) id<PassDelegate>delegate;
@end
TwoViewController.m中
@property (nonatomic, retain) UITextField *tf2;
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"第二頁";
self.tf2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
_tf2.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_tf2];
[_tf2 release];
屬性3: 通過屬性給當前頁內容賦值
self.tf2.text = self.string;
UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];
back.frame = CGRectMake(100, 200, 200, 40);
[back setTitle:@"BACK" forState:UIControlStateNormal];
back.backgroundColor = [UIColor greenColor];
[back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:back];
}
- (void)back
{
協議3: 返回上一頁之前 讓代理人調用協議方法
[self.delegate passValue:self.tf2.text];
[self.navigationController popViewControllerAnimated:YES];
}