ios - 使用 NSNotifications 从 TableViewController 导航回 UIViewController

标签 ios exception uiscrollview delegates uinavigationcontroller

我有一个这样的异常(exception):

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

在我的 Storyboard中,我有 3 个 Controller 。导航 Controller 、UIViewController 和 tableviewcontroller。当应用程序首次启动时,会显示 UIViewController。我向核心数据数据库添加了一些内容。然后在这个 Controller 本身上我有一个“检查记录”栏按钮。我单击它并移动到第三个 Controller ,即 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;
}
}

我在想,如果异常发生在向 ScrollView 添加某些内容时。或者在查看更多堆栈溢出时,可能是因为委托(delegate)或 NSnotification。我无法弄清楚异常发生的原因和位置。编写这行代码是否会出现此异常?

[self.navigationController popViewControllerAnimated:YES];

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

我不知道它在哪里/如何得到这个异常。如果您需要更多信息,请询问。谢谢...

最佳答案

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

只需尝试更改行顺序,首先调用 [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];


}

关于ios - 使用 NSNotifications 从 TableViewController 导航回 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14044441/

相关文章:

iphone - 如何锁定设备上的更改方向?

c++ - 错误 : SymGetLineFromAddr64, GetLastError:487(地址:)

ios - UICollectionView 水平分页仅在第一页上每行显示 3 个单元格

iphone - UIScrollView 中的多个图像

objective-c - UISearchBar 搜索包含文本、副文本和图像的表格行

ios - 来自实用程序类的 sharedInstance 的 presentViewController

ios - 包含重复单元格的 Collection View

java - Java 中的异常

c++ - 在派生类构造函数中抛出异常。为什么调用基类析构函数而不调用派生类析构函数?

objective-c - 方向改变时的 UIScrollview 内容大小