我的 AppDelegate 中有一个名为 handleLocalNotification 的方法,它会在我的应用收到通知时触发。我需要它切换到包含 UITableview 的 UITabBarController 中的选项卡 0。然后我需要它推送到 tableview 的正确行以显示通知发送的记录。所有 Controller 都在 Storyboard中创建,因此我在 AppDelegate 中没有对它们的引用。
我已经添加到我的 AppDelegate.h 中:
@class MyListViewController;
@interface iS2MAppDelegate : UIResponder <UIApplicationDelegate> {
MyListViewController *_listControl;
}
为了测试,我只是把它放在 didFinishLaunchingWithOptions 方法中:
UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
tabb.selectedIndex = 0;
_listControl = [tabb.viewControllers objectAtIndex:0];
[_listControl.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
tabBarController 位有效,因为我可以让它加载到不同的选项卡上。最后两行导致它崩溃。我是否以正确的方式解决了这个问题?或者我需要使用不同的方法吗? 崩溃原因是:
UINavigationController tableView]: unrecognized selector sent to instance
最佳答案
我建议使用 NSNotificationCenter 尝试这样做
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
tabb.selectedIndex = 0;
[[NSNotificationCenter defaultCenter] postNotificationName:@"localNotificationReceived" object:nil];
}
在你的 viewController viewDidLoad 中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectRows) name:@"localNotificationReceived" object:nil];
现在你可以使用你的 tableView 做你的事情了
-(void) selectRows
{
[tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:4 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
要以编程方式选择一个单元格,可以这样做:
NSIndexPath *selectedCellIndexPath = [NSIndexPath indexPathForRow:4 inSection:0];
[table selectRowAtIndexPath:selectedCellIndexPath
animated:YES
scrollPosition:UITableViewScrollPositionTop];
[table.delegate tableView:table didSelectRowAtIndexPath:selectedCellIndexPath];
因为 selectRowAtIndexPath 不会触发表委托(delegate)方法,所以您必须自己调用。
关于objective-c - 从 AppDelegate 更改选项卡和推送 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10267544/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R