简体   繁体   English

条件不起作用

[英]Condition doesn't work

I use button in ViewController to go TableViewController . 我使用ViewController按钮去TableViewController In TableViewController I create int NInt1. TableViewController我创建了int NInt1。 But when I go to TableViewController condition doesn't work. 但是当我转到TableViewController条件不起作用。

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. 现在,在目标视图控制器的viewDidLoad中查看NInt的值。

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 在那个tableviewController viewDidload检查像使用NSLog对象是否为1

If you use storyboard, then the destination view controller is not created from your IBAction . 如果使用情节提要,则不会从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. 而是使用Jaimish答案中所述的prepareForSegue方法。

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;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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