سؤال

I have created two uiviewcontrollers in storyboard. I am adding second UIview as a subview to 1st view when a button is pressed.

Now, my subview has a done and cancel button, which upon been touched, the subview has to be removed from the main view and needs to send some data back to main view. Is using delegates the only way to solve this? Please explain if there is any other simpler or better option.

Thank you :)

هل كانت مفيدة؟

المحلول

It sounds like the question is just about subviews of the 1st view controller's view. In that case, the 1st view controller can directly inspect all of them. i.e. Say the data that you'd like "passed" between views is the text of a UITextField contained on the subview.

You have an outlet to the subview, probably painted in IB?

// MyViewController.m
@property(weak, nonatomic) IBOutlet UIView *subview;  // self.view is it's parent

Create an outlet that connects to whatever subviews you want data from:

@property(weak, nonatomic) IBOutlet UITextField *textField;   // probably, subview is it's parent

Hide and show the "dialog":

self.subview.alpha = 0.0;  // to hide (alpha is better than 'hidden' because it's animatable
self.subview.alpha = 1.0;  // to show

When the button is pressed:

- (IBAction)pressedDoneButton:(id)sender {

     self.subview.alpha = 0.0;

     // or, prettier:
     [UIView animateWithDuration:0.3 animations:^{ self.subview.alpha = 0.0; }];

     // the text field still exists, it's just invisible because it's parent is invisible
    NSLog(@"user pressed done and the text that she entered is %@", self.textField.text);
}

The point is that data is not being passed between views. The view controller has pointers to views. Some, like buttons generate events for the view controller to react to. Others carry data that the view controller can see.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top