0

well is my first post & cannot upload a image because I need a 10 of reputation, XD but it;s ok so:

enter image description here

Hi Guys, well I need pass a label.text from my cell in my tableview from my second or child view controller, to my first or parent view controller, I want push in a cell & go back with the string to load in the label from my first view controller, I did use a segues from storyboard, I did use a protocols & delegates, but not working, in another questions, the answers or examples are contrary to what I need, or is very different way for xcode version, or how is the best way for to do this?, in the picture I use a button for go to the next view controller for choose the label text that I want in my first view controller, Im using xcode 4.6 & storyboard & ARC too, & some ones lines of code are deprecated, well help me please guys!! & thanks a LOT!!!,

greetings from BOLIVIA!!! n_n'

the code is like this:

     @interface FirstViewController : UIViewController <SecondTableViewControllerDelegate>

    @property (copy, nonatomic) NSString *displayText;
    @property (weak, nonatomic) IBOutlet UILabel *displayLabel;
    @property (strong, nonatomic)IBOutlet UIButton *Next;

    @end

    @interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize Next;


    -(void)viewDidLoad
    {
        [super viewDidLoad];
        if (![[NSUserDefaults standardUserDefaults] objectForKey:@"FIRST_TIME"])
        {
            NSLog(@"ES PRIMERA VEZ");
            [Next sendActionsForControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            NSLog(@"NO ES PRIMERA VEZ");
        }


    }

    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];

        if (self.displayText)
        {
            self.displayLabel.text = self.displayText;
        } else {
            self.displayLabel.text = kDefaultDisplayText;
        }
    }

    #pragma mark - Delegate methods

    - (void)updateText:(NSString *)text {
        self.displayText = text;
    }

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if (![segue.identifier isEqualToString:@"toSecondTableview"]) {
            return;
        }
        /*
        SecondTableViewController *destinationController = segue.destinationViewController;
        destinationController.delegate = self;
        destinationController.editText = self.displayText;
        */

        //-- if I pass data from the first view controller to the second right?

    }

    @end

in the second view

    @protocol SecondTableViewControllerDelegate <NSObject>

    @optional
    - (void)updateText:(NSString *)text;

    @end

    @interface SecondTableViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>

    @property (weak, nonatomic) id<SecondTableViewControllerDelegate> delegate;
    @property (copy, nonatomic) NSString *editText;
    @property (weak, nonatomic) IBOutlet UITableView *myTable;


    @interface SecondTableViewController ()

    @end

    @implementation SecondTableViewController

    -(void)viewDidLoad
    {
        [super viewDidLoad];
        [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"FIRST_TIME"];

         //---how use the UItable Methods?, how use the protocol method?
    }


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.myArray.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"myArray";

        myArrayCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        NSDictionary *conjunto = [self.jogArray objectAtIndex:indexPath.row];

        cell.myLabel.text = [conjunto objectForKey:@"prueba"];


        return cell;
    }

or the protocol delegate must be in the other view controller?, I WANT SHOW IN THE LABEL THE TEXT FROM ANY CELL FROM THE NEXT VIEW CONTROLLER HOW AM I TO DO??

user_Dennis_Mostajo
  • 2,279
  • 4
  • 28
  • 38
  • the problem is like this [link](http://g14.picoodle.com/ltd/img14/5/3/5/dennis_mostajo/f_1nyc_5fb_ud7ko.png) – user_Dennis_Mostajo Mar 05 '13 at 15:49
  • Looks fine so far. You just did not actually call `updateText:`. Setting the delegate is commented out. Why?. That is essential for sending the `updateText:` message to the delegate. – Hermann Klecker Mar 05 '13 at 15:55
  • because that code works if I want pass data from first view controller to the second, but not the second to the first – user_Dennis_Mostajo Mar 05 '13 at 16:34
  • Well, that is the point. See my answer. You can use the delegate, which is declared in a protocol, to pass data back to the first view controller. There are more ways for doing so but a delegate, based on a protocol, is a neat and well established solution. – Hermann Klecker Mar 06 '13 at 08:27
  • o well how will be another way to pass back data? to the first viewcontroller without protocol & delegate, exist another method or something? – user_Dennis_Mostajo Mar 15 '13 at 16:14
  • I can suggest that you have a look at this answer - although it is not accepted, you can get the delegation idea: http://stackoverflow.com/questions/9696304/passing-values-from-second-view-to-firstview-in-xcode EDIT: Another answer which is more detailed: http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – o15a3d4l11s2 Mar 05 '13 at 15:53

1 Answers1

0

I would use the delegate pattern. Define a protocol with a method in which the text is handet.

- (void) labelText:(NSString*)text;

or so. Have the partent view controller implementing the protocol. Let's say it is called YourProtocol. In the child view controler declare a delegate property of type (id)<YourProtocol>. In the partent view controller assign self to childControler.delegate. When it is about to assign the text then double check whether the delegate conroms to the protocol or responds to the selctor labelText: and then just call it.

Sounds more complext than it really is and it is sort of 'clean'.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71