如何从 ViewController 传递数据以嵌入视图控制器
How to pass data from ViewController to embed View Controller
我有一个视图控制器,用户在其中点击按钮并移动到第二个视图,我想将一些数据传递给它,但是第二个视图有一个导航视图控制器,所以 prepareForSegue() 在这里不起作用。我应该将数据传递给 Navigation View,然后再传递给 Second View 还是做其他事情?
-(IBAction)moveToSecondViewButtonClicked:(UIButton*)sender {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.data = data;
[self.navigationController pushViewController:secondViewController animated:YES];
}
如果你的数据量适中,可以使用NSNotificationCenter
来传递数据。
发件人Class
- (void)sendNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DATA];
}
接收者class
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"myNotification"
object:nil];
}
- (void)receiveNotification:(NSNotification *)notification
{
if ([[notification name] isEqualToString:@"myNotification"]) {
//doSomething here.
}
}
希望对您有所帮助
不要从 viewcontroller 转到 UIViewController。您必须将 viewcontroller 转至新 UIViewController 的 UINavigationController。您可以在 prepareForSegue 方法中传递数据。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "showNavigationController"{
let navController : UINavigationController = segue.destinationViewController as! UINavigationController
let viewController : UIViewController = navController.viewControllers[0] as! UIViewController
viewController.data = yourData
}
}
据此:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instp/UINavigationController/topViewController
,以下解决方案更正确:使用 UINavitaionController 的 "topViewController"。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let segueID = segue.identifier ?? ""
switch segueID
{
case "toMyNextVC":
let nc = segue.destinationViewController as! UINavigationController
let destinationVC = nc.topViewController as! MyNextViewController
destinationVC.varToPass = myVarToPass
default:
break
}
}
我有一个视图控制器,用户在其中点击按钮并移动到第二个视图,我想将一些数据传递给它,但是第二个视图有一个导航视图控制器,所以 prepareForSegue() 在这里不起作用。我应该将数据传递给 Navigation View,然后再传递给 Second View 还是做其他事情?
-(IBAction)moveToSecondViewButtonClicked:(UIButton*)sender {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.data = data;
[self.navigationController pushViewController:secondViewController animated:YES];
}
如果你的数据量适中,可以使用NSNotificationCenter
来传递数据。
发件人Class
- (void)sendNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DATA];
}
接收者class
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"myNotification"
object:nil];
}
- (void)receiveNotification:(NSNotification *)notification
{
if ([[notification name] isEqualToString:@"myNotification"]) {
//doSomething here.
}
}
希望对您有所帮助
不要从 viewcontroller 转到 UIViewController。您必须将 viewcontroller 转至新 UIViewController 的 UINavigationController。您可以在 prepareForSegue 方法中传递数据。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "showNavigationController"{
let navController : UINavigationController = segue.destinationViewController as! UINavigationController
let viewController : UIViewController = navController.viewControllers[0] as! UIViewController
viewController.data = yourData
}
}
据此:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instp/UINavigationController/topViewController ,以下解决方案更正确:使用 UINavitaionController 的 "topViewController"。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let segueID = segue.identifier ?? ""
switch segueID
{
case "toMyNextVC":
let nc = segue.destinationViewController as! UINavigationController
let destinationVC = nc.topViewController as! MyNextViewController
destinationVC.varToPass = myVarToPass
default:
break
}
}