如何从一个视图控制器导航到另一个视图控制器?

时间:2012-10-10 13:29:22

标签: iphone ios xcode

我有两个视图控制器,在frist视图控制器中我有一个按钮,现在我想用户点击该按钮然后他们将转到第二个视图控制器。我没有使用故事板并且使用ARC,我已经尝试了这个但它显示错误" uiviewcontroller没有可见的@interface声明选择器alloc"。

- (IBAction)SendMsg:(id)sender {

    UIViewController *MessageViewController = [MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
    [self presentModalViewController:MessageViewController animated:YES];
    [MessageViewController release];

}

4 个答案:

答案 0 :(得分:3)

您将“* MessageViewController”声明为UIViewController类型。然后调用“MessageViewController”alloc方法。你不能这样做。 Maulik在正确的轨道上,但如果您使用ARC,则需要删除最后一行([aMessageViewController release];)

尝试:

- (IBAction)SendMsg:(id)sender{
    MessageViewController *myMessageViewControllerInstance = [[MessageViewController alloc]  initWithNibName:@"MessageViewController" bundle:nil];
    [self presentModalViewController:myMessageViewControllerInstance animated:YES];
}

不要忘记你需要一种方法来解雇模态视图控制器,你可以通过在上一行之前添加这样的东西来设置表示和过渡样式:

myMessageViewControllerInstance.modalPresentationStyle = UIModalPresentationFormSheet;
myMessageViewControllerInstance.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

还有其他款式,我只是用过例子。祝你好运

答案 1 :(得分:0)

- (IBAction)SendMsg:(id)sender {

    MessageViewController *aMessageViewController = [[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
    [self presentModalViewController:aMessageViewController animated:YES];
    //[aMessageViewController release]; As you are using ARC

}

答案 2 :(得分:0)

    - (IBAction)SendMsg:(id)sender {
        MessageViewController *aMessageViewController = [[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
        [self presentModalViewController:aMessageViewController animated:YES];
    }

答案 3 :(得分:0)

    - (IBAction)SendMsg:(id)sender {

MessageViewController *aMessageViewController = [[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
[self presentModalViewController:aMessageViewController animated:YES];
[aMessageViewController release];

}

如果您使用导航控制器,则可以使用self.navigationController .......也。