使用NSNotifications从TableViewController导航回UIViewController [英] Navigating back to UIViewController from TableViewController using NSNotifications
问题描述
我有一个例外:
Thread 1: EXC_Breakpoint (code = EXC_I386_BPT,subcode=0x0)
在我的故事板中,我有3个控制器。导航控制器,UIViewController,然后一个tableviewcontroller。当应用程序首次启动UIViewController时显示。我向Core Data DB添加了一些东西。然后在这个控制器本身我有一个检查记录栏按钮。我点击它,并移动到第三个控制器的tableviewcontroller。在这里我可以看到当天添加的记录。我点击了我刚刚添加的一个,并使用NSNotifications遍历到UIviewcontroller屏幕,如下所示:
In my story board i have 3 controllers. Navigation controller, UIViewController and then a tableviewcontroller. When the app first launches UIViewController is shown. I add some stuff to the Core Data DB. Then on this controller itself i have a "Check Records" bar button. I click that and move to the 3rd controller the tableviewcontroller. Here i can see the records that i added on that day. I click the one i just added and traverse back to the UIviewcontroller screen using NSNotifications like this:
//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中,我写这个代码来收听通知。 p>
In my ViewController viewDidLoad i write this code to listen to the notification.
[[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。我不明白为什么和发生异常的地方。写这行代码给我这个例外?
I am thinking if the exception occurs at adding something to scroll view.Or on looking some more on stack overflow it could be either because of delegate or NSnotification. I cant figure out why and where the exception occurs.Does writing this line of code give me this exception?
[self.navigationController popViewControllerAnimated:YES];
这个问题是这个问题的扩展
将自定义视图添加到UIScrollview会导致滚动时卡住的屏幕
this question is extension of this question Adding Customview to UIScrollview results in stuck screen when Scrolling
我不知道它在哪里/如何得到这个异常。如果您需要更多信息,请问。谢谢...
I have no idea where/how its getting this exception. If you need more info please ask. Thanks...
推荐答案
有一件事可能是当你调用 newReloadData
您通知通知中心哪些类具有 @reloadRequest
字符串与 newReloadData
方法应该运行它,所以你需要首先弹出视图,然后调用通知
One thing might be when you call newReloadData
you notify notification center that what class has@"reloadRequest"
string with newReloadData
method should run it, so you need to first pop the view then call the notification
只需尝试更改行顺序,首先调用 [self.navigationController popViewControllerAnimated:YES];
然后调用 [[NSNotificationCenter defaultCenter] postNotification:notification];
Just try change the line order first call [self.navigationController popViewControllerAnimated:YES];
then call [[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];
}
这篇关于使用NSNotifications从TableViewController导航回UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!