0

i have two view controller firstviewconroller and secondviewcontroller. i have alert view in firstviewconroller now i want to goto second secondviewcontroller. clicking alert view button. guide me how to call secondviewcontroller using code.i'm new to this stuff. here is my alert view code.

-(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.");
    }
}

now its showing console output like this.i don't know where i'm doing wrong.

-[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'
  • Are you using a `UINavigationController`? Do you want it pushed onto that or do you want it to appear modally? You need to give a bit more context. Also I suggest reading about `UINavigationController` and `presentModalViewController:animated:` - they are your friends here. – mattjgalloway Dec 09 '11 at 11:35
  • You should try to avoid comparing the button titles to find out which button was pressed. This will come back to you once you localize your app, or you just change the name of one button. – tonklon Dec 09 '11 at 11:38
  • @mattjgalloway no mattjgalloway i'm using normal view based uiviewcinroller. –  Dec 09 '11 at 11:39
  • @moor87 Ok, so how do you want the `SecondViewController` to appear then? What do you want to actually do? – mattjgalloway Dec 09 '11 at 11:40
  • @mattjgalloway i want to call SecondViewController using alert view button.how to call.like android intent calling one activity to another activity(start activity).like that. –  Dec 09 '11 at 11:45
  • @moor87 Yes, but the view hierarchy in iOS is very different to Android. There's a few different ways of doing what you want. And there's no `finish()` like in Android so you have to work out how you're going to go back to `FirstViewController` (assuming you want to, that is). – mattjgalloway Dec 09 '11 at 12:12
  • Instead of writting this :- UIViewController *secondViewController = [[Hello_WorldViewController alloc] initWithNibName:@"" bundle:nil]; [self Page3:secondViewController animated:YES]; [secondViewController release]; try to write :- Hello_WorldViewController *secondViewController = [[Hello_WorldViewController alloc] initWithNibName:@"Hello_WorldViewController" bundle:nil]; [self presentModalViewController:secondViewController animated:YES]; [secondViewController release]; – mAc Dec 12 '11 at 04:59

3 Answers3

1

This is the code,

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

There are several questions with the same topic, you should have search before posting this question.

KingofBliss
  • 15,055
  • 6
  • 50
  • 72
  • thank you for your reply srry for this same kind topic.now i edited my question. same code showing exception.i don't where i'm done mistake. –  Dec 09 '11 at 12:21
  • @moor87 Change the code [self Page3:secondViewController animated:YES]; as [self presentModalViewController:secondViewController animated:YES]; – KingofBliss Dec 12 '11 at 05:17
0

i did this but app crashes on the line

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=@"";
  }

which i have a login screen

and on the FirstListView i have two table views. having no idea why the app crashes.

0

This is Very Simple You just have to create an Object of the NextViewController on which you want to go and call presentModalViewController on that.. like this.:-

in 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.");
    }
}

And Yes What KingofBliss Said is very true. Please make it in Notice. Good Day.

mAc
  • 2,434
  • 2
  • 22
  • 39
  • yes i tried can't get it i'm using xpcode 4.2. so now i done some other way using segue story bored identifier. –  Dec 12 '11 at 05:09
  • 1
    So, Is it done..?? By the way i m using 4.1. According to the error you have wriiten here... it is saying that you have called some wrong method on Button click try to write the call on button in different way.. it should work. All the Best. :)) – mAc Dec 12 '11 at 05:23