2

I have this code :

-(IBAction)OkButtonPressed:(id)sender{
    NSLog(@"BTN OK");
    RecherchePartenaireTableView *recherchePartenaireTableView=[[RecherchePartenaireTableView alloc]init];
    recherchePartenaireTableView.mytext=textFieldCode.text;

    [self.navigationController popViewControllerAnimated:YES];
}

and after I press ok I see in console the message "BTN OK" and nothing else. In class RecherchePartenaireTableView I have the methods viewWillAppear, viewDidload... and a NSLog message for each method. What method is called after [self.navigationController popViewControllerAnimated:YES];?

Gabrielle
  • 4,933
  • 13
  • 62
  • 122

3 Answers3

4

If you have a controller A and you are pushing controller B on top of A. So on calling popViewControllerAnimated in controller B

viewWillAppear:animated for controller A will be called

in your case B is RecherchePartenaireTableView, so there is no way viewWillAppear for B will be called on doing popViewController.

If you want to do something when RecherchePartenaireTableView disappears, do it in RecherchePartenaireTableView's viewWillDisappear

Nilesh Ukey
  • 5,488
  • 3
  • 20
  • 22
  • I want to set a text to a label after RecherchePartenaireTableView is called again.. – Gabrielle Sep 21 '11 at 11:33
  • 1
    You should use a model, cause you are saving state. IF you dont want to do that you can create a statevariable in appdelegeate, [(AppDelegate *)[UIApplication sharedApplication].delegate stateVar] = someValue, and use this value in viewWillAppear for setting text – Nilesh Ukey Sep 21 '11 at 11:35
4

If you are trying to set a property of class RecherchePartenaireTableView, which is on the navigation stack already then you are doing it wrong by creating a new instance of it.

You should be getting back the instance from navigationController stack.

Change

RecherchePartenaireTableView *recherchePartenaireTableView=[[RecherchePartenaireTableView alloc]init];
recherchePartenaireTableView.mytext=textFieldCode.text;

To

NSArray *viewControllers = [self.navigationController viewControllers];
RecherchePartenaireTableView *recherchePartenaireTableViewVC = (RecherchePartenaireTableView *)[viewControllers objectAtIndex:viewControllers.count - 2];
recherchePartenaireTableViewVC.mytext=textFieldCode.text;

viewDidAppear method will be called on the class you pushed the view from.

Gavin
  • 2,784
  • 6
  • 41
  • 78
  • I found this solution [navigationController viewWillAppear:YES]; on viewDidLoad – Gabrielle Sep 21 '11 at 12:02
  • Why textFiledCode.text is null? how can I take the text from a textField?...now I have and this problem : on viewWillappear mytext is null and I don't know why :| – Gabrielle Sep 21 '11 at 12:04
  • What are you trying to do actually? Set a property(myText) of class `RecherchePartenaireTableView` from the class(class your button was on) that was pushed ontop of it? – Gavin Sep 21 '11 at 12:10
  • In RecherchePartenaireTableView I have the property @property (nonatomic, retain) NSString *mytext; In RecherchePartenaireTypePartenaireViewController I have this : -(IBAction)OkButtonPressed:(id)sender{ NSLog(@"BTN Ok"); RecherchePartenaireTableView *recherchePartenaireTableView=[[RecherchePartenaireTableView alloc] init]; recherchePartenaireTableView.mytext=cellText; NSLog(@"jjjjjj %@",recherchePartenaireTableView.mytext); [self.recherchePartenaireTableView.tableviewrecherchepartenaire reloadData]; [self.navigationController popViewControllerAnimated:YES]; } – Gabrielle Sep 21 '11 at 12:16
  • The problem is that on viewwillAppear myText is null..why? – Gabrielle Sep 21 '11 at 12:17
  • See edited answer for the changes you need to made. If it's null on viewWillAppear, try it on viewDidAppear. – Gavin Sep 21 '11 at 12:21
  • because you are recreating a new instance of RecherchePartenaireTableView class, everytime you do perfrom your IBAction, fist of all this is a memory leak as you are not releasing your object and creating it again and again, rather you should have a property RecherchePartenaireTableView * recherchePartenaireTableView on RecherchePartenaireTypePartenaireViewController, and just set the text when IBAction is performed, (also you should reload your table in setter for myText in RecherchePartenaireTableView) – Nilesh Ukey Sep 21 '11 at 12:24
  • if I don't put RecherchePartenaireTableView *recherchePartenaireTableView=[[RecherchePartenaireTableView alloc] init]; in my IBAction, recherchePartenaireTableView.mytext is null after recherchePartenaireTableView.mytext=cellText... – Gabrielle Sep 21 '11 at 12:27
  • And on viewWillAppear it is still null – Gabrielle Sep 21 '11 at 12:30
  • Can you provide me an example ? – Gabrielle Sep 21 '11 at 12:30
  • Ok, so `RecherchePartenaireTableView` is a UITableView not a UIViewController. Firstly created a NSString property on the VC that the tableview is on. Set this NSString during your IBAction. And load this string to your cell on your cellForRowAtIndex path. Reload your tableview everytime viewDidAppear. – Gavin Sep 21 '11 at 12:30
  • @Gabrielle what is this myText? Please be more specify in what you are trying to do. – Gavin Sep 21 '11 at 12:33
  • @property (nonatomic, retain) NSString *mytext; on RecherchePartenaireTableView – Gabrielle Sep 21 '11 at 12:35
  • I tried to use this : http://stackoverflow.com/questions/7076762/how-to-send-the-text-from-textfield-to-another-class – Gabrielle Sep 21 '11 at 12:37
  • @RecherchePartenaireTableView is a ViewController or UITableView? You really need to follow some naming conversion. I am confused by your namings. – Gavin Sep 21 '11 at 12:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3660/discussion-between-gabrielle-and-matakazer) – Gabrielle Sep 21 '11 at 12:41
  • RecherchePartenaireTableView is a ViewController – Gabrielle Sep 21 '11 at 12:49
  • Did you try my answer? I should work. Try `NSLog(@"%@", self.mytext")` during viewDidAppear. You might have missed out `self.mytext = [[NSString alloc] init]; during viewDidLoad at RecherchePartenairTableView(viewController)`. Don't forget to do `self.mytext = nil` during dealloc. – Gavin Sep 21 '11 at 13:07
  • You right...I forgot self.mytext = [[NSString alloc] init]; ...I put NSLog(@"%@", self.mytext") during viewDidAppear, but nothing is show. :( – Gabrielle Sep 21 '11 at 13:18
  • Please do an NSlog on recherchePartenaireTableViewVC just before the statement of setting the property. What does it return? I forget an autorelease statement when we init mytext. `self.mytext = [[[NSString alloc] init] autorelease];` – Gavin Sep 21 '11 at 13:24
  • I have this: -(IBAction)OkButtonPressed:(id)sender{ NSLog(@"BTN Ok"); recherchePartenaireTableView=[[RecherchePartenaireTableView alloc] init]; NSLog(@"before %@",recherchePartenaireTableView.mytext); recherchePartenaireTableView.mytext=cellText; NSLog(@"after %@",recherchePartenaireTableView.mytext); [self.navigationController popViewControllerAnimated:YES]; } @end – Gabrielle Sep 21 '11 at 13:29
  • before I get null and after I get the cellText – Gabrielle Sep 21 '11 at 13:30
  • Do a NSLog(@"%@",[self.navigationController viewControllers]); You can't do `[[RecherchePartenaireTableView alloc] init];`. It's a different instance. Therefore it wouldn't be reflected back when you popViewController. – Gavin Sep 21 '11 at 13:33
  • I have a little question: how can I take the text from a textField? I used [textField text] and I get null – Gabrielle Sep 21 '11 at 13:39
  • No problem. Anyway, you should learn to follow naming conversion. UIViewController should end with ViewController. So your RecherchePartenaireTableView should be RecherchePartenaireTableViewViewController instead. It's a good practice to follow. Makes troubleshooting and coding easier. – Gavin Sep 21 '11 at 13:39
  • You need to create a UITextField during interface. `IBOutlet UITextField *myTextField;` Make sure the textField is linked up properly on interface builder. – Gavin Sep 21 '11 at 13:42
  • IDK if using `textField.text` would make a differences. I personally use this and not [textField text]; – Gavin Sep 21 '11 at 13:47
  • OMG i never thought this will work out , but it did , Great. Thanks a Lot . +1 for u :) (y) – ishhhh Jan 10 '14 at 11:10
2
- (void) viewWillAppear: (BOOL)animated method will be called first
Maulik
  • 19,348
  • 14
  • 82
  • 137
MohanVydehi
  • 356
  • 3
  • 14
  • then why I don't get the message from NSLog? – Gabrielle Sep 21 '11 at 11:15
  • IF that doesn't work you can use the UINavigationControllerDelegate method called - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated; – MohanVydehi Sep 21 '11 at 11:19