xcode 如何将视图推送到另一个视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11609733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:59:07  来源:igfitidea点击:

how to push a view to another view

objective-ciosxcodeuibuttonstoryboard

提问by Rinju Jain

How can I connect my button to another view controller class programmatically

如何以编程方式将我的按钮连接到另一个视图控制器类

here is code for my programmatically button

这是我的编程按钮的代码

UIBarButtonItem *yearButton= [[UIBarButtonItem alloc] initWithTitle:@"Year" style:UIBarButtonItemStyleBordered    
 target:self action:@selector(year:)];


-(void) year:(id)sender{
    NSLog(@"Year button clicked");

    //I don't know what should I write here to connect my button to UIViewController**
    //when I added this line my process is terminated** 

    [self performSegueWithIdentifier:@"YearView" sender:self]; 
 }

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString: @"YearView"]){
       [segue.destinationViewController setTitle:@"YearView"];
    }
}

Here you can see my view in storyboard: ![enter image description here][1]

在这里您可以在故事板中看到我的视图:![在此处输入图像描述][1]

Edit:*My process is Terminated when I used this method*

编辑:*当我使用此方法时,我的进程被终止*

-(void)year:(id)sender{

// [self performSegueWithIdentifier:@"YearView" sender:self]; 
NSLog(@"Year button clicked");
YearView *yearVC = [[YearView alloc] initWithNibName:@"YearView" bundle:nil];

[[self navigationController] pushViewController:yearVC animated:YES];   // [yearVC release];

}

回答by Elnaz

Try this one :

试试这个:

YearView *year = [[YearView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:year animated:YES];

It's good to read the tutorial first: Presenting View Controllers from Other View Controllers http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

最好先阅读教程:从其他视图控制器呈现视图控制器 http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

回答by Rinju Jain

UIBarButtonItem *yearButton = [[UIBarButtonItem alloc] initWithTitle:@"Year"style:UIBarButtonItemStyleBordered target:self action:@selector(year:)];
self.navigationItem.rightBarButtonItem = yearButton;

-(void) year:(id)sender {
    NSLog(@"Year button clicked");
    YearViewcontroller *yearVC = [[YearViewcontroller alloc] initWithNibName:@"YearViewcontroller" bundle:nil];
    [self.navigationController  pushViewController:yearVC  animated:YES];
    [yearVC release];
}

回答by Jorge Aguirre

To link your button with the method in the viewController, you need to set an IBOutlet in the interface designer (this is the simplest way). If you still want that in another view controller, you could set a property of the another viewController for example in the viewDidLoad method, so that the other viewController also has a reference to the button, altough that is far from ideal. Only one viewController should manage the button.

要将按钮与 viewController 中的方法链接起来,您需要在界面设计器中设置一个 IBOutlet(这是最简单的方法)。如果你仍然希望在另一个视图控制器中使用它,你可以设置另一个视图控制器的属性,例如在 viewDidLoad 方法中,这样另一个视图控制器也有对按钮的引用,尽管这远非理想。只有一个 viewController 应该管理按钮。