iOS:目前的视图控制器programmatically
我使用presentViewController:animated:completion:
方法转到另一个视图控制器。
这是我的代码:
AddTaskViewController *add = [[AddTaskViewController alloc] init]; [self presentViewController:add animated:YES completion:nil];
这段代码进入另一个UIViewController
但另一个控制器是空的。 我一直在使用故事板,但现在我需要在代码中完成。
如果你使用的是故事板,你可能不应该使用alloc
和init
来创build一个新的视图控制器。 相反,看看你的故事板,find你想要执行的继续; 它应该有一个唯一的标识符(如果没有,你可以在右边栏中设置一个)。
一旦你find了那个segue的标识符,发送你当前的视图控制器-performSegueWithIdentifier:sender
message:
[self performSegueWithIdentifier:@"mySegueIdentifier" sender:self];
这将导致故事板实例化一个AddTaskViewController,并以您为该segue定义的方式呈现它。
另一方面,如果你根本不使用故事板,那么你需要给你的AddTaskViewController某种用户界面。 这样做的最常见方式是用一个nib初始化控制器:不是调用init
,而是调用-initWithNibName:bundle:
并提供一个包含你的添加任务UI的.xib文件的名称:
AddTaskViewController *add = [[AddTaskViewController alloc] initWithNibName:@"AddTaskView" bundle:nil]; [self presentViewController:add animated:YES completion:nil];
(还有其他(不太常见的)获得与你的新视图控制器相关的视图的方法,但是这可能会使你工作的麻烦最less。
如果您正在使用Storyboard,并且“添加”viewController在故事板中,则在设置中为您的“添加”视图控制器设置一个标识符,以便您可以执行如下操作:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"NameOfYourStoryBoard" bundle:nil]; AddTaskViewController *add = [storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"]; [self presentViewController:add animated:YES completion:nil];
如果在storyboard或nib文件中没有“添加”viewController,并且想要创build整个程序,那么appDocs会说:
如果无法在故事板或nib文件中定义视图,请重写loadView方法以手动实例化视图层次并将其分配给视图属性。
你的代码:
AddTaskViewController *add = [[AddTaskViewController alloc] init]; [self presentViewController:add animated:YES completion:nil];
这个代码可以到另一个控制器,但是你得到一个新的viewController,而不是你的故事板的控制器,你可以这样做:
AddTaskViewController *add = [self.storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardID"]; [self presentViewController:add animated:YES completion:nil];
您需要从故事板身份检查器中设置故事板ID
AddTaskViewController *add=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard_id"]; [self presentViewController:add animated:YES completion:nil];
尝试以下操作:
NextViewController *nextView = [self.storyboard instantiateViewControllerWithIdentifier:@"nextView"]; [self presentViewController:nextView animated:YES completion:NULL];
试试这个代码:
[self.navigationController presentViewController:controller animated:YES completion:nil];
最好的办法是
AddTaskViewController * add = [self.storyboard instantiateViewControllerWithIdentifier:@"addID"]; [self presentViewController:add animated:YES completion:nil];
这里是你如何在Swift中做到的:
let vc = UIViewController() self.presentViewController(vc, animated: true, completion: nil)
LandingScreenViewController *nextView=[self.storyboard instantiateViewControllerWithIdentifier:@"nextView"]; [self presentViewController:nextView animated:YES completion:^{}];
- IBOutlet在两个故事板上的两个viewController …是可能的
- 在后台接收并响应EKEventStoreChangedNotification?
- 在scrollView中的ImageView中裁剪放大的图像
- 我的iOS应用程序没有在模拟器中显示应用程序图标
- Cordova – 不推荐尝试访问非导航器对象上的属性“userAgent”
- 优步无效的OAuth 2.0凭据提供Uber身份validation在iOS Swift中
- 在scrollview中切换StackVery的IsVisible在Xamarin表单iOS中不能正确更新
- 在故事板中收集视图的末尾添加button
- iOS – 更改UIBarButtonItem的高度