在 Xcode 5.0.2 中,我为 iPhone 创建了一个空白的 Master Detail 应用程序,它在模拟器中运行良好:
当 iPhone 应用程序正在启动或从后台唤醒时,我想在其中间显示一个带有标签“正在加载...”的模态视图,获取网页(在这个测试用例中;在真实的应用程序,这将是游戏更新和玩家分数),然后关闭网页上的模态视图获取完成或错误或超时。
所以我创建了 2 个新文件,LoadingViewController.h和 LoadingViewController.m (我现在没有自定义代码)。
因为它是 Xcode 版本 5,所以没有 xib 文件,只有一个 Main.storyboard - 所以我从对象库中将一个 View Controller 拖到 Storyboard上。然后在右侧,我选择了 LoadingViewController 类作为身份检查器中的自定义类:
最后我在 AppDelegate.m 中添加了 3 个方法:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self showLoadingView];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[self dismissLoadingView];
}
- (void)showLoadingView
{
NSLog(@"%s", __PRETTY_FUNCTION__);
[self fetchHttp];
LoadingViewController *other = [[LoadingViewController alloc] init];
[self.window.rootViewController presentViewController:other animated:YES completion:nil];
}
- (void)dismissLoadingView
{
NSLog(@"%s", __PRETTY_FUNCTION__);
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)fetchHttp
{
NSLog(@"%s", __PRETTY_FUNCTION__);
NSString *urlAsString = @"http://stackoverflow.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] > 0 &&
error == nil) {
NSString *html = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %u", [html length]);
}
else if ([data length] == 0 &&
error == nil) {
NSLog(@"Nothing was downloaded.");
}
else if (error != nil) {
NSLog(@"Error happened = %@", error);
}
// XXX how to dismiss the modal view here, it's a different thread?
}];
}
不幸的是,现在我在模拟器中得到了一个黑屏和以下输出:
2013-12-01 22:37:01.332 LoadingTest[3840:a0b] -[AppDelegate showLoadingView]
2013-12-01 22:37:01.334 LoadingTest[3840:a0b] -[AppDelegate fetchHttp]
2013-12-01 22:37:01.857 LoadingTest[3840:a0b] Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x8c74f10>.
2013-12-01 22:37:01.870 LoadingTest[3840:4607] HTML = 196885
我很难理解如何在这里使用 Storyboard(如果可能的话,我想使用它)——因为我正在阅读的书(在 O'Reilly Safari 中)都在谈论 xib 文件(可能适用于较旧的 Xcode 版本?)。
而且我不明白如何从我的 completionHandler 中关闭模态视图,因为它在不同的线程中,我可能不应该从那里调用 dismissViewControllerAnimated?
更新:
我已将“ Storyboard ID”添加到我的 View :loadingView 并将以下代码添加到 AppDelegate.m:
- (void)showLoadingView
{
NSLog(@"%s", __PRETTY_FUNCTION__);
[self fetchHttp];
UIStoryboard *board = [self.window.rootViewController storyboard]; //[UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];
LoadingViewController *other = [board instantiateViewControllerWithIdentifier:@"loadingView"];
[self.window.rootViewController presentViewController:other animated:YES completion:nil];
}
- (void)fetchHttp
{
NSLog(@"%s", __PRETTY_FUNCTION__);
NSString *urlAsString = @"http://stackoverflow.com";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] > 0 &&
error == nil) {
NSString *html = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %u", [html length]);
}
else if ([data length] == 0 &&
error == nil) {
NSLog(@"Nothing was downloaded.");
}
else if (error != nil) {
NSLog(@"Error happened = %@", error);
}
[self dismissLoadingView];
}];
}
但现在我收到下面的警告并且 loadingView 没有被关闭(可能是因为网页加载速度比显示的模态视图快?):
2013-12-03 01:49:12.208 LoadingTest[631:70b] -[AppDelegate showLoadingView]
2013-12-03 01:49:12.210 LoadingTest[631:70b] -[AppDelegate fetchHttp]
2013-12-03 01:49:12.756 LoadingTest[631:70b] HTML = 200949
2013-12-03 01:49:12.757 LoadingTest[631:70b] -[AppDelegate dismissLoadingView]
2013-12-03 01:49:12.757 LoadingTest[631:70b] Warning: Attempt to dismiss from view controller <UINavigationController: 0x8a70ce0> while a presentation or dismiss is in progress!
2013-12-03 01:49:12.844 LoadingTest[631:70b] Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0x8a70ce0>.
最佳答案
首先,当您在 Storyboard中实例化 Controller 时,您不使用 alloc init,而是使用 UIStoryboard 方法 instantiateViewControllerWithIdentifier:。你需要给你的 Controller 一个“ Storyboard ID”,我可以从你的图像中看到你还没有完成(如果你不理解 Storyboard,请阅读 Apple 的文档)。
您可以从完成处理程序中关闭模态视图——处理程序是在异步操作完成后调用的代码,因此您应该使用 [NSOperationQueue mainQueue] 作为队列参数。
关于ios - 向 Master-Detail 应用程序添加模式加载 View (在 applicationDidBecomeActive 方法中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20317589/
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我需要从一个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编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr