我是iPhone应用程序开发的新手。在我的代码中,在第一个ViewController1中,我有一个文本字段和一个ok按钮。所以这里
当我没有将ok按钮连接到Interface Builder中的ViewController2时,我能够正确地完成第一部分。但是当我连接时,它会导航然后显示警报消息。 请输入字符串并按确定按钮后,请告诉我如何以编程方式导航到ViewController2。
这是我的代码查找Ok按钮的方式:
-(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];
}
答案 0 :(得分:3)
基于Segue的导航:
删除segue连接OK按钮到View Controller 2并在View Controller 1和View Controller 2之间添加一个手动segue。然后执行以下代码:
-(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];
}
}
基于代码的导航:
-(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];
}
}
这假设您正在使用ARC并且foldername1
是一个强大的ivar / property。
答案 1 :(得分:1)
如果将segue分配给按钮,则不应该给它一个单击处理程序。只需从按钮中删除segue,代码即可,因为您使用pushViewController:animated:
进行导航而不是使用segue进行导航。
通常,您将从视图控制器1创建一个segue到viwe controller 2,使用标识符命名它,并使用performSegueWithIdentifier:sender:
方法在代码中激活它。像这样:
[self performSegueWithIdentifier:@"selectedIdentifierForTheSegue" sender:self];
有关详细信息,这是一个非常好的实施导航教程:http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
答案 2 :(得分:0)
刚刚减记
return;
<警告显示下面的;然后它只显示一个警报,而不是导航到下一个视图。
答案 3 :(得分:0)
试试这个
(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];
}
答案 4 :(得分:0)
推送视图控制器:
ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2"
bundle:nil];
[self.navigationController pushViewController:vc2 animated:YES];
并检查this。