关闭模态视图不起作用

时间:2011-12-09 23:28:00

标签: uitableview modal-dialog viewcontroller

我一直把头发拉出来。我正在创建一个非常简单的应用程序,它只是下载一个rss feed并将其显示在UITavigationController中的UITableview中。虽然它正在下载Feed我正在呈现一个模态视图。

在我的模态视图中,我正在显示一个UIImageView和一个设置为旋转的UIActivityIndi​​catorView。我正在使用ASIHTTRequest来异步获取feed,然后使用完成块来获取响应字符串并停止微调器或失败块以获取NSError并显示警报View。这一切都很完美。

然后我创建了一个协议来从tableview中解除模态视图,该视图在完成块内部被调用。但模态观点永远不会被驳回!我已经尝试将其推入导航控制器,但出现了完全相同的问题。我甚至尝试将模态视图委托设置为nil但仍然没有运气。

我使用ASIHTTPRequest委托方法检查了它没有块,并且它是相同的,如果我没有提供模态视图,表视图会正常显示。

任何想法?我已经跳过了所有tableview委托和数据源方法以及dealloc和任何未使用的函数。

@interface MainTableViewController () 
-(void)loadModalView;
@end


@implementation MainTableViewController
@synthesize tableView;
@synthesize modalView;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{

   [super loadView];
tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];

   [self loadModalView];


}

-(void)loadModalView
{
    modalView = [[ModalViewController alloc]init];
    modalView.delegate = self;
    [self presentModalViewController:modalView animated:NO];

}


//Modal View Delegate
-(void)downloadComplete
{
modalView.delegate = nil;
[self dismissModalViewControllerAnimated:NO];



}


@end 


@interface ModalViewController ()


- (void)loadView
{
[super loadView];

backgroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];

[self.view addSubview:backgroundImage];

spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(160, 240, spinner.bounds.size.width, spinner.bounds.size.height);
spinner.hidesWhenStopped = YES;
[self.view addSubview:spinner];
[spinner startAnimating];


NSString* urlString = FEED_URL;
NSURL* url = [NSURL URLWithString:urlString];

ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    [spinner stopAnimating];
    [delegate downloadComplete];
    // Use when fetching binary data

}];
[request setFailedBlock:^{
    NSError *error = [request error];
    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Error" message:error.description delegate:self cancelButtonTitle:@"Continute" otherButtonTitles: nil];
    [alert show];
    [alert release];
}];
[request startAsynchronous];



}

马特

2 个答案:

答案 0 :(得分:0)

根据我的理解......你的解决方案非常复杂.. 如果类MainTableViewController是,那会不会更好 为ModalView下载Feeds的人,它将作为ActivityIndi​​cator充当并在下载后解散.. 在你的MainTableViewController加载视图中:

- (void)loadView
{
  NSString* urlString = FEED_URL;
  NSURL* url = [NSURL URLWithString:urlString];

  ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
  [request startAsynchronous];
  //after starting the request show immediately the modalview
  modalView = [[ModalViewController alloc]init];
  [self presentModalViewController:modalView animated:NO];

  [request setCompletionBlock:^{
    // Use when fetching text data
    NSString *responseString = [request responseString];
    //then when it is complete dissmiss the modal
    [modalView dismissModalViewControllerAnimated:NO];

    // Use when fetching binary data
  }];
  [request setFailedBlock:^{
    NSError *error = [request error];
    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Error" message:error.description delegate:self cancelButtonTitle:@"Continute" otherButtonTitles: nil];
    [alert show];
    [alert release];
  }];


}

我没有在我的项目中使用块,但我认为它将工作相同.. 我也使用普通的UIActivityIndi​​catorView(大)作为子视图而不是modalViews ..遗憾的是我现在不能在这里测试代码..但我可以稍后检查它

答案 1 :(得分:0)

我解决此错误的唯一方法是同步下载数据并将下载视图推送并弹出到导航堆栈。不理想,但它有效。