简体   繁体   中英

Condition doesn't work

I use button in ViewController to go TableViewController . In TableViewController I create int NInt1. But when I go to TableViewController condition doesn't work.

ViewController.m

- (IBAction)Button:(id)sender {
MasterViewController *trns =[[MasterViewController alloc]init];
[_Button.titleLabel.text isEqualToString:@"1"];
    trns.NInt1=1;
}

TableViewController.m

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(_NInt1 == 1){
    return 5;
}else{
    return 20;
}
}

If you are using the story pass the value like this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    RecipeDetailViewController *destViewController = segue.destinationViewController;
    destViewController.NInt1 = 1;
}

Now see the value of NInt in viewDidLoad of your destination view controller.

I think you need to modify like

- (IBAction)Button:(UIButton *)sender {
 MasterViewController *trns =[[MasterViewController alloc]init];
if [sender.titleLabel.text isEqualToString:@"1"]{
  trns.NInt1=1;
 }
}

on that tableviewController viewDidload check like use NSLog for the object is 1 or not

If you use storyboard, then the destination view controller is not created from your IBAction . It can be verified easily : you never push or present the newly created view controller, which is local in your method.

Instead use prepareForSegue method as described in Jaimish answer.

I tried the solution.Finally I got.Thank you.The condition has worked now.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if([segue.identifier isEqualToString:@"goTableViewController"])
  {
    TableViewController *tableVC = (TableViewController *) segue.destinationViewController;
    tableVC.NInt1 = 5;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM