Make UIButton act as navigationcontroller

后端 未结 2 674
小蘑菇
小蘑菇 2021-01-28 21:18

How can I make a regular UIButton act as a navigationcontroller so that when it\'s pressed I can open up a new view?

相关标签:
2条回答
  • 2021-01-28 21:38

    Assuming you are using a UINavigation controller in your project.

    then for the buttons action just call

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

    and for the method that gets called just do the following:

    -(void)buttonAction{
        [self.navigationController pushViewController:YourViewController animated:YES];
    }
    
    0 讨论(0)
  • 2021-01-28 21:49

    Create yourButton in viewDidLoad method as following way...

    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];
    

    This is your method code and createViewController which is object of CreateViewController class and it is declared in .h file.....

    -(void) buttonSelected:(id)sender{
       if (!createViewController) {
                    createViewController = [[CreateViewController alloc] initWithNibName:@"CreateViewController" bundle:nil];
    
                }
    
    
                [self.navigationController pushViewController:createViewController animated:YES];
    
     }
    

    I think it will be helpful to you.

    0 讨论(0)
提交回复
热议问题