Reputation: 59
I am new to iPhone apps development. In my code, in first ViewController1 I have a textfield and a ok- button. So here
I am able to do the 1st part correctly when I have not connected the ok-button to the ViewController2 in Interface Builder. But when I connect, it navigates and then shows alert message. Please tell me how to navigate to ViewController2 programmatically after I enter the string and press ok button.
This is how my code looks for Ok- button:
-(IBAction) okButton: (id) sender
{
if([textfield.text isEqualToString:@""])
{
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"Warning!!" message:@"Enter the file name" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else if(textfield.text!= nil)
{
foldername1= [[NSString alloc] initWithString:[NSString stringWithString:textfield.text]];
NSLog(@"foldername is: %@", foldername1);
}
ViewController2 *view2= [[ViewController2 alloc] initWithNibName:@"Next View Name" bundle:nil];
[self.navigationController pushViewController:view2 animated:YES];
}
Upvotes: 1
Views: 1761
Reputation: 9012
Segue-based navigation:
Remove the segue connecting OK button to View Controller 2 and add a manual segue between View Controller 1 and View Controller 2. Then implement the following code:
-(IBAction) okButton: (id) sender
{
if(textfield.text.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!!"
message:@"Enter the file name"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
foldername1 = textfield.text;
NSLog(@"foldername is: %@", foldername1);
[self performSegueWithIdentifier:@"segueIdentifier" sender:self];
}
}
Code-based navigation:
-(IBAction) okButton: (id) sender
{
if(textfield.text.length == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning!!"
message:@"Enter the file name"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
else
{
foldername1 = textfield.text;
NSLog(@"foldername is: %@", foldername1);
ViewController2 *view2 = [[ViewController2 alloc] initWithNibName:@"Next View Name"
bundle:nil];
[self.navigationController pushViewController:view2 animated:YES];
}
}
This assumes you're using ARC and that foldername1
is a strong ivar / property.
Upvotes: 3
Reputation: 6718
To push view controller:
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2"
bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
And also check this.
Upvotes: 0
Reputation: 1044
try this
(IBAction) okButton: (id) sender
{
if(textfield.text.length==0)
{
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"Warning!!" message:@"Enter the file name" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else if(textfield.text.length!=0)
{
foldername1= [[NSString alloc] initWithString:[NSString stringWithString:textfield.text]];
NSLog(@"foldername is: %@", foldername1);
}
ViewController2 *view2= [[ViewController2 alloc] initWithNibName:@"Next View Name" bundle:nil];
[self.navigationController pushViewController:view2 animated:YES];
}
Upvotes: 0
Reputation: 2430
just writedown
return;
below [alert show]; line then it just shows an alert and not navigate to the nextview.
Upvotes: 0
Reputation: 16790
If you assign a segue to a button, then you should not give it a click handler. Simply remove the segue from the button and the code should be Ok since you are using pushViewController:animated:
to navigate instead of navigating with a segue.
Normally you would create a segue from view controller 1 to viwe controller 2, name it with an identifier, and activate that in code with the performSegueWithIdentifier:sender:
method. Like this:
[self performSegueWithIdentifier:@"selectedIdentifierForTheSegue" sender:self];
For more info this is a pretty good tutorial for implementing navigation: http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
Upvotes: 1