2 iphone model-view-controller uitableview uinavigationcontroller
我有一个UITableView,在其中我以下面的方式创建一个自定义UITableViewCell:
ItemCellController *cell = (ItemCellController *)[tableView dequeueReusableCellWithIdentifier:ContentListsCellIdentifier];
...
cell = [[[ItemCellController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ContentListsCellIdentifier] autorelease];
Run Code Online (Sandbox Code Playgroud)
我这样做可以获得touchesBegan和touchesEnded事件(这样我就可以实现长时间的触摸).使用NSLog我可以看到使用以下代码从touchesBegan方法中正确调用longTouch:
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(longTouch:) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
问题是我无法从longTouch方法中调用模态窗口.
我尝试了以下,但我得到一个NSInvalidArgumentException - [ItemCellController navigationController]:无法识别的选择器发送到实例错误.
AddItemController *addView = [[AddItemController alloc] initWithNibName:@"AddItemView" bundle:nil];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:addView];
controller.navigationBar.barStyle = UIBarStyleBlack;
[[self navigationController] presentModalViewController:controller animated:YES];
[controller release];
Run Code Online (Sandbox Code Playgroud)
所以问题是,如何在自定义UITableViewCell中调用模态窗口.
谢谢
Tyl*_*ler 13
该navigationController属性存在于UIViewControllers,但UITableViewCell(我猜它ItemCellController是其子类)不是a UIViewController,因此默认情况下它没有该属性.
有一些appraoches:
(1)将UIViewController*属性(可能称之为)添加controller到自定义单元格类型,并使用init方法(例如,initWithController:)将指针传递给控制器.然后在你的单元格中,你可以执行:
UINavigationController* navController = [ /* alloc and init it */ ]
[self.controller presentModalViewController:navController animated:YES];
Run Code Online (Sandbox Code Playgroud)
(2)您的app委托对象可以具有您可以从代码中的任何位置访问的控制器属性.然后你可以做这样的事情:
MyAppDelegate* myAppDelegate = [[UIApplication sharedApplication] delegate];
[myAppDelegate.controller presentModalViewController:navController
animated:YES];
Run Code Online (Sandbox Code Playgroud)
(3)这个不那么直接但更灵活.您可以设置根控制器(您想要显示模态视图的控制器)来监听某个通知,并从表格单元格中发布该通知.
从根控制器中调用的示例侦听代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(showModal:)
name:@"show modal"
object:nil];
Run Code Online (Sandbox Code Playgroud)
从表格单元格中调用的示例邮政编码:
NSDictionary* userInfo = [ /* store a handle to your modal controller */ ];
[[NSNotificationCenter defaultCenter] postNotificationName:@"show modal"
object:self
userInfo:userInfo];
Run Code Online (Sandbox Code Playgroud)
并且showModal:根控制器的方法将使用userInfo包含在中的方法NSNotification来确定哪个视图控制器以模态方式呈现.这是更多的工作,但它会自动允许任何代码在任何地方呈现模态视图,而不会让他们完全访问根控制器指针.
| 归档时间: |
|
| 查看次数: |
5696 次 |
| 最近记录: |