您的位置:首页 > 产品设计 > UI/UE

UINavigationController 与其传值

2015-08-08 22:05 363 查看
准备工作

MainViewControll *mainAC = [[MainViewControll alloc] init];
UINavigationController *ngaAC = [[UINavigationController alloc] initWithRootViewController:mainAC];
self.window.rootViewController = ngaAC;


导航视图控制器的高度是44, 上面的状态栏的高度是20, 加在一起的默认高度是64.

给导航视图控制器加上标题

self.title = @"何以笙萧默";

或者:

self.navigationItem.title = @"微微一笑很倾城";


背景设置

// 并不是所有的背景都是backgroundColor

self.navigationController.navigationBar.barTintColor = [UIColor blackColor];

// 为了防止坐标系被串改,我们把Bar从半通明设置成不透明,这样坐标系的圆点就回自动向下推64.

self.navigationController.navigationBar.translucent = NO;

// 指定一些视图, 成为titleView

UISegmentedControll *seg = [[UISegmentedControl alloc] initWithItems:@[@"信息", @"通话"]];
self.navigationTtem.titleView = seg;

// 创建左右两边的按钮

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(liftButtonACtion:)] autorelease];

self.navigationItem.rghtBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"图片名"] style:UIBarButtonItemStylePlain target:self action:@selector(方法名)] autorelease];

或者:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 40, 40);
[rightButton setImage:[UIImage imageNamed:@"照片名"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];


属性传值

UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(200, 100, 100, 40);
button.layer.borderWidth = 1;
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];
self.myTextField.layer.borderWidth = 1;
self.myTextField.layer.cornerRadius = 10;
[self.view addSubview:self.myTExtField];
[_myTextField release];

- (void)buttonAction:(UIButton *)button
{
SecondViewController *secondAC = [[secondViewController alloc] init];
// 模块跳转
secondAC.modalTransitionStyle = UIModalTransitionStyleCoverVertical];
[self presentViewController:secondAC animated:YES completion:^{

}];
// 导航视图控制器跳转

[self.navigationController pushViewController:secondAC animated:YES];
[secondAC release];

secondAC.str = self.myTestField.Text;
}


协议传值

// 1. 声明一份协议
@protocol SecondViewControllerDelegate<NSObject>

// xieyifangf
- (void)changeValue:(NSString *)value;

@end

@interface SecondViewController : UIViewController
// 2. 设置代理人的属性
@property(nonatomic, assign)id<secondViewControllerDelegate>delegade;

@end

// 协议的触发条件是点击按钮, 所以在这里进行协议传值的第3部
// 3. 设置代理人的协议方法
- (void)buttonAction:(UIButton *)button
{
[self.navigationController popViewControllerAnimated:YES];
[self.delegate changeValue:self.textfied.text];
}

// 4. 签订协议
@interface mainViewController()<SecondViewControllerDelegate>

@end

// 5. 设置代理人
- (void)buttonAction:(UIButton *)button
{
SecondViewController *secondAC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondAC animated:YES];

secondAC.delegate = self;
}

// 6. 实现协议方法
- (void)changeValue:(NSString *)value
{
self.lable.text = value;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: