问题描述:

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

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??

网友答案:

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'.

相关阅读:
Top