执行 segue 时视图出现两次 [英] View appearing twice when performing segue

查看:32
本文介绍了执行 segue 时视图出现两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用有问题.我正在使用导航控制器和 TableView.所以在我的第一个视图中,我有一个名为 propTableView 的 TableView,它是一个属性列表.当我单击一个单元格时,它会将我带到另一个 ViewController 和另一个名为 propDetailTableView 的 TableView.我执行从 PropViewControllerPropDetailViewController 的 Segue:

I have an issue with my app. i'm using Navigation Controller and TableView. So in my first View I have a TableView named propTableView, it's a list of property. When I click on a cell it brings me to an other ViewController with an other TableView named propDetailTableView. I perform a Segue from PropViewController to PropDetailViewController :

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *sectionTitle = [aromaPropSectionTitles objectAtIndex:indexPath.section];
    NSArray *sectionProp = [propData objectForKey:sectionTitle];
    propName = [sectionProp objectAtIndex:indexPath.row];
    [self performSegueWithIdentifier:@"PropDetail" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"PropDetail"]) {
        PropViewController *propView = segue.destinationViewController;
        propView.title = propName;
        SQLmanager *sqlManager = [[SQLmanager alloc] initDatabase:@"prop_max.sqlite3"];
        NSString *propRequest = [NSString stringWithFormat:@"SELECT id_prop FROM prop_max WHERE prop_name_fr LIKE '%@'", propName];
        NSLog(@"propRequest = %@", propRequest);
        NSString *propId = [sqlManager getString:propRequest];
        NSLog(@"propId = %@", propId);
    }
}

现在我只是更改导航栏的标题.但行为并不正常.不是显示带有新标题的 PropDetailViewController,而是显示带有先例标题的新 PropDetailViewController(如果这是我第一次单击单元格,它不显示标题),然后显示 PropDetailViewController 与标题.这不会有问题,但是当我单击左上角的 Back 时,它会将我带回带有旧标题的 PropDetailViewController,我必须单击Back 第二次返回PropViewController.

For now I just change the Title of the Navigation Bar. But the behaviour is not normal. Instead of displaying the PropDetailViewController with the new title it displays the new PropDetailViewController with the precedent title (if it's the first time I click on a cell it displays no title), and then it displays the PropDetailViewController with the title. It wouldn't be a problem, but when I click on Back in the upper left it bring me back to the PropDetailViewController with the old title, and I have to click a second time on Back to return to the PropViewController.

我点击了一个单元格(我之前点击了 Anti-Douleur,但现在我选择了 Actif-Cosémtique):

I click on a cell (I clicked on Anti-Douleur before, but now I choose Actif-Cosémtique):

它显示:

然后一秒钟后:

推荐答案

啊,对不起,我知道问题出在哪里了.

Ah sorry I know what the problem is.

就是这么简单:

prepareForSegue 在设置 UIView 之前运行.

prepareForSegue runs WAY BEFORE THE UIView IS SET UP.

它只涉及视图控制器,而不涉及视图.

It only concerns the view controller, not the view.

你需要做的是...

在转场期间设置一些信息......

SET SOME INFORMATION during the segue....

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
  if ([[segue identifier] isEqualToString:@"idUber"])
    {
    Uber *uber = [segue destinationViewController];
    uber.classnameToUse = .. whatever ...;
    }
  }

然后在那个其他类中......稍后,当 viewDidLoad 运行时,然后使用该信息.

and then in that other class... MUCH LATER, WHEN viewDidLoad RUNS, then use that information.

注意 - viewDidAppear 可能更合适,具体取决于您的情况

note - viewDidAppear may be more appropriate, depending on your situation

-(void)viewDidLoad
  {
  [self loadThisMainSection:self.classnameToUse];

  // note that back during the segue you have CORRECTLY SET .classnameToUse
  // that was a long time ago.  but we only use it now.
  }

就是这么简单!

您必须在每种情况下都这样做.

You have to do this in every segue situation.

你最终会得到像whenYouCanUseThisValue"这样的变量名....

You end up with variable names like "whenYouCanUseThisValue"....

在您的情况下,您需要一个名为:

In your case you need a property called:

@property (strong) NSString *useThisTitleLaterWhenYouAreLoading;

顺便说一句,不要忘记,如果它是非类属性,则是赋值"

BTW don't forget that if it is a non-class property, it's "assign"

@property (assign) CGPoint afterLoadingHereIsThePosition;
@property (assign) CGFloat onceYouLoadUseThisAlpha;

这篇关于执行 segue 时视图出现两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆