I have a View Controller, where user taps button and moves to Second view, and I want to pass some data to it, but Second view has a Navigation View Controller, so prepareForSegue() doesn't work here. Shall I pass data to Navigation View and then to Second View or do something else?
Asked
Active
Viewed 1,990 times
1
-
I think http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers it can help you. – User18474728 Aug 25 '15 at 06:38
4 Answers
4
According to this: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instp/UINavigationController/topViewController , the following solution is more correct: use "topViewController" of the UINavitaionController.
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
}
}

Maris
- 664
- 1
- 5
- 15
0
-(IBAction)moveToSecondViewButtonClicked:(UIButton*)sender {
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.data = data;
[self.navigationController pushViewController:secondViewController animated:YES];
}

Destiny
- 171
- 6
0
If your data size is moderate, you can use NSNotificationCenter
for passing the data.
Sender Class
- (void)sendNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:YOUR_DATA];
}
Receiver 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.
}
}
Hope this helps

iAnurag
- 9,286
- 3
- 31
- 48
0
Dont segue from your viewcontroller to UIViewController. you must segue viewcontroller to UINavigationController of new UIViewController. You can pass data in prepareForSegue method.
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
}
}

muhammedkasva
- 672
- 4
- 12
- 20