按下相同的控件时按下 segue,使我的应用程序崩溃

Posted

技术标签:

【中文标题】按下相同的控件时按下 segue,使我的应用程序崩溃【英文标题】:Pushing segue when pressing same control, crashes my app 【发布时间】:2014-04-24 15:24:16 【问题描述】:

这里发生了一些奇怪的事情。我有一个tableview,每一行都有一个segmentedcontrol。其含义是,始终只需要选择一个分段控件。单击“控制项”时,会推送一个 segue。

如果我不断选择不同分段控件的项目,则不会发生任何事情,我的代码可以完美运行。当我从同一个分段控件中选择不同的项目时,我有这个错误:

嵌套推送动画可能导致导航栏损坏 在意外状态下完成导航转换。导航栏子视图树可能会损坏。 开始/结束外观转换的不平衡调用。

这是我的代码:

@interface PerformancesController ()

    GFilm* currentFilm;
    GTimePerformance* chosenTimePerf;

@end

@implementation PerformancesController



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    // Return the number of sections.
    return 1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section


    // Return the number of rows in the section.
return [currentFilm.dayList count];



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath

    UITableViewCell *cell = [tableView     dequeueReusableCellWithIdentifier:@"myCustomCell"];
    if (cell == nil) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCustomCell"];
    


CAGradientLayer *bgLayer = [BackgroundLayer blackGradient];
bgLayer.frame = cell.bounds;
[cell.contentView.layer addSublayer:bgLayer];
cell.contentView.backgroundColor = [UIColor darkGrayColor];
cell.textLabel.textAlignment = UITextAlignmentLeft;
cell.selectionStyle = UITableViewCellSelectionStyleNone;

if(indexPath.row == 0)

    CGRect rctStreep = cell.bounds;
    rctStreep.origin.y = rctStreep.origin.y+1;
    rctStreep.size.height = 1;
    UIImageView* imgv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"streep.png"]];
    imgv.backgroundColor = [UIColor blackColor];
    imgv.frame = rctStreep;
    [cell.contentView addSubview:imgv];




CGRect rctStreep = cell.bounds;
rctStreep.origin.y = rctStreep.origin.y + rctStreep.size.height;
rctStreep.size.height = 1;
UIImageView* imgv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"streep.png"]];
imgv.backgroundColor = [UIColor blackColor];
imgv.frame = rctStreep;
[cell.contentView addSubview:imgv];
imgv = nil;

NSArray* dayList = currentFilm.dayList;
GDay* d = (GDay*)[dayList objectAtIndex:indexPath.row];


CGRect rctDayLabel = CGRectMake(15, 7, 80, 30);
UILabel* lblDay = [[UILabel alloc]initWithFrame:rctDayLabel];
lblDay.textColor = [GColor yellowAccentColor];
NSDate* date = d.date;


if([date isToday])

    lblDay.text = @"Vandaag";

else if([date isTomorrow])

    lblDay.text = @"Morgen";

else

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM"];
    lblDay.text = [formatter stringFromDate:date];


[cell.contentView addSubview:lblDay];

NSMutableArray* itemArray = [[NSMutableArray alloc]init];
int i = 0;
GTimePerformance* prevChosen = currentFilm.displayPerformance;
int gekozen = -1;

for(i; i < [d.timeIdList count]; i++)


    GTimePerformance* per = [d.timeIdList objectAtIndex:i];
    if([per.performanceId isEqualToString:prevChosen.performanceId])
    
        gekozen = i;
    

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"];
    [itemArray addObject:[formatter stringFromDate:per.performanceTime]];




float itemWidth = (cell.frame.size.width - (2* rctDayLabel.origin.x))/4;

MySegmentedControl *segmentedControl = [[MySegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(rctDayLabel.origin.x, cell.frame.size.height - rctDayLabel.origin.y - 30, itemWidth*[d.timeIdList count] , 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.tintColor = [GColor filterComponentColor];
if(gekozen >= 0)

    [segmentedControl setSelectedSegmentIndex:gekozen];

[segmentedControl addTarget:self
                     action:@selector(clickedPerformance:)
           forControlEvents:UIControlEventValueChanged];
UIFont *font = [UIFont systemFontOfSize:10.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                       forKey:NSFontAttributeName];
[segmentedControl setTitleTextAttributes:attributes
                                forState:UIControlStateNormal];
segmentedControl.tag = 200 + indexPath.row;
[cell.contentView addSubview:segmentedControl];

return cell;




-(void)clickedPerformance:(id)sender
 

    UISegmentedControl *segment = (UISegmentedControl*)sender;

   if(segment.selectedSegmentIndex != -1)
   
       int tag = segment.tag;
       int i = 0;
       for(i; i < [currentFilm.dayList count]; i++)
       
           int diffTag = i + 200;
           if(diffTag != tag)
           
               UISegmentedControl* sControl = (UISegmentedControl*)[self.view viewWithTag:diffTag];
           [sControl setSelectedSegmentIndex:UISegmentedControlNoSegment];
       
   

   NSLog(@"lalala");
   GDay* day = (GDay*)[currentFilm.dayList objectAtIndex:tag-200];
   chosenTimePerf = [day.timeIdList objectAtIndex:segment.selectedSegmentIndex];
   NSLog(chosenTimePerf.performanceId);
   [self performSelector:@selector(goSegue) withObject:nil afterDelay:0.35];

  

-(void)goSegue

    [self performSegueWithIdentifier:@"pushPrices" sender:self];



// In a storyboard-based application, you will often want to do a little preparation     before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

     NSString * segueName = segue.identifier;
     if ([segueName isEqualToString: @"pushPrices"]) 
     PricesController* pc = (PricesController*)[segue destinationViewController];
    [pc setMovie:currentFilm andPerformance:chosenTimePerf];





@end

【问题讨论】:

【参考方案1】:

为什么要延迟goSegue 方法?在afterDelay 过去之前多次点击该项目会导致麻烦。

我会改变:

[self performSelector:@selector(goSegue) withObject:nil afterDelay:0.35];

到:

[self goSegue];

【讨论】:

我推迟了它,因为我认为它可以解决问题。因为也许控制还没有完全设置好。也许是一个愚蠢的解释,但我对移动编程相当陌生:)。 @user3484771 我没有看到延迟转场的正当理由,你尝试过我的建议吗 我想我解决了这个问题。当按下同一个控件时,[self goSegue] 会被调用两次。奇怪 是的,我试过了,问题仍然存在,这是我之前没有想到的错误,见上文。

以上是关于按下相同的控件时按下 segue,使我的应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

在点击按钮时按下导航控制器

按下页面时执行 Segue

我创建了一个添加两个数字的应用程序但是当编辑文本为空时按下按钮的应用程序一直停止

为啥添加导航控制器会使我的数组消失?

iOS:如何检测使用 BT 键盘时按下的任何键

无法使我的 ListView 可选择,按下该元素不会突出显示