1

I am a newbie in xcode and still learning its very basics. If my question feels too basic, please bear with me.

I have a view, which is my main view. When user clicks on a button (Options Button), I want to go to a second view. Here user can select one option from many available.

Once user selected his option, I want to send this value back to first view and resume processing from where it was (when user clicked the Options Button).

Important - User will be moving from view1 to view2 in the mid of processing. I have to join back from view2 to view1 and resume from exactly where I left off... this means, i need all variable values to be available.

GoGauchos
  • 588
  • 5
  • 11
user2734323
  • 706
  • 1
  • 8
  • 19

3 Answers3

0

Easy! All you have to do is control drag from your UIButton to the new view. Select Modal.

enter image description here

Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70
  • Hmm... That just won't help. The problem is that, when I come back to view one, my session will not resume instead it starts from the beginning... Just to give you an idea, user will be moving from screen 1 to screen 2 in the middle of the processing. Therefore, it is important that I should be able to resume from where I left... including all instant variables.... – user2734323 Sep 04 '13 at 00:26
  • @user2734323 Try embedding your first view controller in a navigation controller and doing a push segue – Abdullah Shafique Sep 04 '13 at 00:28
  • Tried that... but that will always create a new instance of the view controller. – user2734323 Sep 04 '13 at 00:33
0

Sounds like you need to implement a delegate. I'm not the best at explaining protocols and delegates, so I'll provide links that talk about them. I will provide a rough implementation below.

Protocols https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html

Delegates https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html#//apple_ref/doc/uid/TP40010810-CH11

Difference between the two Difference between protocol and delegates?

View2.h

@protocol View2;

@interface View2 : UIViewController

@property (nonatomic, weak) id<View2Delegate> *delegate;

...

@end

@protocol View2Delegate

-(void)view2Finished:(NSString *)value;

@end

View2.m

@synthesize delegate

-(void)backButtonPressed
{
    [delegate view2Finished:@"Value to be passed to view 1"];
}

View1.h

// This code says that View1 implements View2Delegate
@interface View1 : UIViewController<View2Delegate>

View1.m

-(void)view2Finished:(NSString *)value
{
    NSLog(@"Value received from view 2: %@", value);
}
Community
  • 1
  • 1
Dylan Bettermann
  • 753
  • 8
  • 14
0

A push segue won't create a new version of the parent view controller. Are you starting the session processing in viewDidLoad or in viewDidAppear / viewWillAppear? Are you stopping processing in viewWillDisappear? In viewWillDisappear, you can check the navigation controller's stack to see if your parent view has been covered or is being popped from the stack using this code snippet:

- (void)viewWillDisappear:(BOOL)animated {

    if ([[[self navigationController] viewControllers] indexOfObject:self] == NSNotFound) {
       // we are disappearing.  Clean up the session.
        ...
    }
    [super viewWillDisappear:animated];
}

You can store (limited amounts of) state in the view controller, using UIViewController's encodeRestorableStateWithCoder: method - check the documentation. I don't use it myself but it could help.

By the way, you may find it easier to use UIViewController's done:, reset: and canPerformUnwindSegueAction:fromViewController:withSender: rather than create delegate protocols. Unwind segues were introduced in iOS 6 and require less code than delegates.

kmorris
  • 654
  • 4
  • 6
  • A push seque won't create a new version of the view controller, but it will instantiate the destination view before displaying it. That was my issue. It seems, canPerformUnwindSegueAction:fromViewController:withSender is the way to go forward. Thanks for suggesting it. I gave it a shot and it is working great! – user2734323 Sep 04 '13 at 13:44