视图加载后处理方法

发布于 2024-09-05 03:15:00 字数 216 浏览 3 评论 0原文

我已经实现了一个子视图,当我单击父视图中的按钮时,该子视图应该立即加载。加载子视图(基本上包含活动指示器)后,程序应该处理其中的方法(从服务器获取数据,因此需要时间)。

但是,我无法做到这一点。

现在发生的情况是,当我单击父视图上的按钮时,它首先处理该方法,只有在这之后子视图才会加载到屏幕上。

为什么会这样呢?是否有任何特定的函数可以用来使我的方法仅在视图加载后加载?

I have implemented a subview which is supposed to load immediately when I click a button in the parent view. After loading the subview(which is basically holding an activityindicator), the program is supposed to process a method(which gets data from a server, so takes time) in it.

However, I am unable to do it.

What happens now is, when I click the button on the parent view, it processes the method first and only after that does the subview load on screen.

Why is this so? Is there any specific functions I could use to make my method load only after the view has loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哎呦我呸! 2024-09-12 03:15:00

我遇到了同样的问题,我使用了 NSAutoreleasePool 并解决了这个问题。我希望它能帮助你。

- (void)viewDidLoad {

   [self performSelectorInBackground:@selector(loadXml) withObject:nil];

 }

 -(void) loadXml{

        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

       NSString * path =@"http:www.YOUR_RSS_FEED.com";

       [self parseXMLFileAtURL:path];       //(Instead of ViewDidAppear)

      [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

       [pool drain];

   }

谢谢。

I have faced the same problem and i have used NSAutoreleasePool and solved this problem. I hope it will help you.

- (void)viewDidLoad {

   [self performSelectorInBackground:@selector(loadXml) withObject:nil];

 }

 -(void) loadXml{

        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

       NSString * path =@"http:www.YOUR_RSS_FEED.com";

       [self parseXMLFileAtURL:path];       //(Instead of ViewDidAppear)

      [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

       [pool drain];

   }

Thanks.

薔薇婲 2024-09-12 03:15:00

几种不同的方法:

如果子视图具有加载图像和执行比视图更复杂的功能,那么一个好的方法是将其放入 ViewController 中。

在顶部 ViewController 中使用:

- (void) loadView { 
    [super loadView];
}

设置显示视图时需要准备的东西。

用途:

-(void) viewDidLoad {

}

进行附加设置。

在您的情况下,听起来好像您需要在 loadView 方法中添加指示器,然后在 viewDidLoad 方法中开始检索数据。

如果您正在访问 Web 服务等,请始终在不同的线程上执行此操作。 (查看 NSOperation 以获得实现此目的的良好、简单的方法)。

A few different approaches:

If the subview has functionality as loading an image and doing more complicated stuff than just being a view, a good approach is to make it into a ViewController instead.

In the top ViewController use:

- (void) loadView { 
    [super loadView];
}

to set up things that need to be ready upon displaying the view.

The use:

-(void) viewDidLoad {

}

to do additional setup.

In your case it sounds as if you need to add the indicator in the loadView method, then start the retrieval of data in the viewDidLoad method.

If you are accessing web services etc. always do this on a different thread. (look into NSOperation for a good, simple way of achieving this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文