如何在Xcode 4.2中将一个视图控制器调用到另一个视图控制器

时间:2011-12-09 11:33:36

标签: iphone uiviewcontroller xcode4.2

我有两个视图控制器firstviewconroller和secondviewcontroller。我在firstviewconroller中有警报视图现在我想转到第二个secondviewcontroller。单击警报视图按钮。指导我如何使用code.i'm新的东西来调用secondviewcontroller。 这是我的警报视图代码。

-(IBAction)enter:(id) sender{
    UIAlertView *alertBox=[[UIAlertView alloc]initWithTitle:@"ThinkTax!" message:@"0.0" delegate:self cancelButtonTitle:@"Button 1" otherButtonTitles:nil];

    [alertBox addButtonWithTitle:@"Sve"];
    [alertBox addButtonWithTitle:@"Button 3"];
    if(FALSE)
    {
        [alertBox addButtonWithTitle:@"Button 4"];
    }

    [alertBox show];
    [alertBox release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"details"])
    {
 UIViewController *secondViewController = [[Hello_WorldViewController alloc] initWithNibName:@"<Hello_WorldViewController >" bundle:nil];
    [self Page3:secondViewController animated:YES];
    [secondViewController release];

            NSLog(@"Button details was selected.");
    }
    else if([title isEqualToString:@"mail"])
    {

        NSLog(@"Button mail was selected.");
    }
    else if([title isEqualToString:@"close"])
    {

        NSLog(@"Button close was selected.");
    }
}

现在它显示控制台输出像this.i不知道我在哪里做错了。

-[Page3 Page3:animated:]: unrecognized selector sent to instance 0x8d26ba0
Hello World[5961:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Page3 Page3:animated:]: unrecognized selector sent to instance 0x8d26ba0'

3 个答案:

答案 0 :(得分:1)

这是代码,

UIViewController *secondViewController = [[SecondViewControllerClass alloc] initWithNibName:@"<name of xib>" bundle:nil];
[self presentModalViewController:secondViewController animated:YES];
[secondViewController release];

同一主题有几个问题,您应该在发布此问题之前进行搜索。

答案 1 :(得分:0)

这很简单你只需要创建一个你想去的NextViewController的Object并在那个上调用presentModalViewController ......就像这样。: -

在FirstVC.h中

: -

@class SecondVC;

    SecondViewController *viewController;

inFirstViewController.m: -

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"details"])
    {
      viewController = [[SecondViewController alloc]     initWithNibName:@"SecondViewController" bundle:nil];
      [self presentModalViewController:viewController animated:YES];
      [viewController release];
    }
    else if([title isEqualToString:@"mail"])
    {

        NSLog(@"Button mail was selected.");
    }
    else if([title isEqualToString:@"close"])
    {

        NSLog(@"Button close was selected.");
    }
}

是的,KingofBliss说的是非常真实的。请在通知中进行。 美好的一天。

答案 2 :(得分:0)

我做了这个,但应用程序崩溃了

if([str isEqualToString:@"xxxxx"])
{
if([str1 isEqualToString:@"xxxxx"])
   {
   FirstListView *ab=[[FirstListView alloc]initWithNibName:@"FirstListView" bundle:nil];         
   [self presentModalViewController:ab animated:NO];//here app crashes
   }

   }
   else
  {
    user.text=@"Wrong User";
    password.text=@"";
  }

我有一个登录屏幕

在FirstListView上我有两个表视图。 不知道应用程序崩溃的原因。