使UIButton充当导航控制器

时间:2012-08-31 10:40:40

标签: ios uinavigationcontroller uibutton

如何使常规UIButton充当导航控制器,以便在按下时我可以打开一个新视图?

2 个答案:

答案 0 :(得分:0)

假设您在项目中使用导航控制器。

然后按钮动作只需调用

[yourUIButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

并且对于被调用的方法,只需执行以下操作:

-(void)buttonAction{
    [self.navigationController pushViewController:YourViewController animated:YES];
}

答案 1 :(得分:0)

按照以下方式在viewDidLoad方法中创建yourButton ...

UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setTitle:@"YearButton" forState:UIControlStateNormal];
yourButton.frame = CGRectMake(240, 40, 75, 30);     
[yourButton addTarget:self action:@selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:yourButton];

这是你的方法代码和createViewController,它是CreateViewController类的对象,它在.h文件中声明.....

-(void) buttonSelected:(id)sender{
   if (!createViewController) {
                createViewController = [[CreateViewController alloc] initWithNibName:@"CreateViewController" bundle:nil];

            }


            [self.navigationController pushViewController:createViewController animated:YES];

 }

我认为这会对你有所帮助。