使用NSNotifications从TableViewController导航回UIViewController

时间:2012-12-26 18:14:03

标签: ios exception uiscrollview delegates uinavigationcontroller

我有这样的例外:

Thread 1: EXC_Breakpoint (code = EXC_I386_BPT,subcode=0x0)

在我的故事板上我有3个控制器。导航控制器,UIViewController然后是tableviewcontroller。当应用程序首次启动时,会显示UIViewController。我在Core Data DB中添加了一些东西。然后在这个控制器本身我有一个“检查记录”栏按钮。我点击它并移动到第3个控制器tableviewcontroller。在这里,我可以看到我当天添加的记录。我点击我刚刚添加的那个并使用NSNotifications遍历到UIviewcontroller屏幕,如下所示:

//This code is in tableviewcontroller.didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ITMAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.salesOrderObject = [self.salesOrdersArray objectAtIndex:indexPath.row];

    NSNotification *notification =[NSNotification notificationWithName:@"reloadRequest"
                                                                object:self];
    [[NSNotificationCenter defaultCenter] postNotification : notification];

    [self.navigationController popViewControllerAnimated:YES];
}

在我的ViewController viewDidLoad中,我编写此代码来收听通知。

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(newReloadData)
                                             name:@"reloadRequest"
                                           object:nil];


-(void)newReloadData
{
self.salesOrder =  self.appDelegate.salesOrderObject; //self.reloadedSOObject; //appDelegate.salesOrderObject;

if(self.salesOrder !=nil)
{
//I add UItextviews,textfields,buttons etc here to a scrollview.
ITMCustomView *cView = [[ITMCustomView alloc] initWithFrame:CGRectMake(187, 660, 400, 400)
                                                     andOrderedItem:@"New"
                                                             andTag:self.tagNumber
                                                      withFoldArray:self.foldTypeArray
                                                      withRollArray:self.rollTypeArray];

        cView.tag =self.tagNumber;
        NSLog(@"Assigned tag is %d",cView.tag);

        cView.delegate = self;

        //[self.scrollView addSubview:customView];

        //self.scrollView.contentSize = CGRectMake(187, 660, 400, 400).size;


        CGPoint scrollPoint = CGPointMake(0.0, (cView.frame.origin.y/500)*400);
        [self.scrollView setContentOffset:scrollPoint animated:YES];

        [self.scrollView addSubview:cView];

        CGFloat scrollViewHeight = 0.0f;
        for (UIView *view in self.scrollView.subviews)
        {
            scrollViewHeight += view.frame.size.height;
        }

        CGSize newSize = CGSizeMake(320,scrollViewHeight);
        //CGRect newFrame = (CGRect){CGPointZero,newSize};
        [self.scrollView setContentSize:newSize];
        NSLog(@"750 the tag number is %d",self.tagNumber);
        NSLog(@"the scroll view height is %f",scrollViewHeight);
        self.currentCGRectLocation = cView.frame;
}
}

我在想如果在添加滚动视图的内容时发生异常。或者在查看堆栈溢出时,可能是因为委托或NSnotification。我无法弄清楚异常发生的原因和地点。编写这行代码会给我这个例外吗?

[self.navigationController popViewControllerAnimated:YES];

这个问题是这个问题的延伸 Adding Customview to UIScrollview results in stuck screen when Scrolling

我不知道它在何处/如何获得此异常。如果您需要更多信息,请询问。感谢...

1 个答案:

答案 0 :(得分:1)

有一件事可能是当你致电newReloadData时通知通知中心哪个类有@"reloadRequest"字符串newReloadData方法应该运行它,所以你需要先弹出视图然后调用通知

只需尝试更改第一次调用行[self.navigationController popViewControllerAnimated:YES];,然后调用[[NSNotificationCenter defaultCenter] postNotification : notification];

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ITMAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.salesOrderObject = [self.salesOrdersArray objectAtIndex:indexPath.row];

    NSNotification *notification =[NSNotification notificationWithName:@"reloadRequest"
                                                                object:self];
    //here first pop view
    [self.navigationController popViewControllerAnimated:YES];

    //then send notification before scopes end
    [[NSNotificationCenter defaultCenter] postNotification : notification];


}