使用dismissModalViewControllerAnimated刷新上一个视图

时间:2013-09-06 04:22:34

标签: iphone ios uiview

我正在提出一个视图说B使用

 [self presentViewController:vc animated:YES completion:nil];

从这个视图我关闭它以回到使用

查看A.
  [self dismissModalViewControllerAnimated:YES];

现在,当我回去时,我想调用reload previous view(这里是视图A)。视图A是具有来自服务器的数据列表的tableview。所以reloadtable不起作用,因为新数据只能来自服务器。所以简而言之,我想在关闭View B时调用该服务器并刷新该视图。

如何做到这一点。?

我尝试在ViewWillAppear视图A的方法中调用ViewDidLoad,但它没有用。

请帮忙。

编辑: 我的服务器调用就像这样

 - (void)viewWillAppear:(BOOL)animated
 {
   NSString *path = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"path"];

    NSString *address = [NSString stringWithFormat:@"%@%@%d%@%@", path,@"questions/?cat=",self.category,@"&que_language=",language];

NSString* encodedUrl = [address stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];

NSURL *URL = [NSURL URLWithString:encodedUrl];
NSLog(@"%@",address);

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
                                                   timeoutInterval:60.0];

[request setHTTPMethod:@"GET"];

responseData = [[NSMutableData alloc] init];

// [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
//Write code you want to call when data is received,

NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (data) {
   NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
    int statuscode = [httpResponse statusCode];
    NSString *responseMsg = [NSHTTPURLResponse localizedStringForStatusCode:statuscode];
    switch (statuscode) {
         case 200:{
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"data:%@",responseString);

            SBJsonParser *parser = [[SBJsonParser alloc] init];
            NSMutableArray *object = [parser objectWithString:responseString error:nil];

            questionList = [[NSMutableDictionary alloc] init ];
            questions = [[NSMutableDictionary alloc] init ];
            que_id = [[NSMutableDictionary alloc] init ];
            ans_count = [[NSMutableDictionary alloc] init ];

            int i=0;

            for (NSDictionary *quelist in object) {

                NSString *que = [quelist objectForKey:@"que_content"];
                NSString *queId = [NSString stringWithFormat:@"%@",[quelist objectForKey:@"question_id"]] ;
                NSString *ansCount = [NSString stringWithFormat:@"%@",[quelist objectForKey:@"ans_count"]] ;

                [self.questions setValue:que forKey:[NSString stringWithFormat:@"%d",i]];
                [self.que_id setValue:queId forKey:[NSString stringWithFormat:@"%d",i]];
                [self.ans_count setValue:ansCount forKey:[NSString stringWithFormat:@"%d",i]];

                [self.questionList setValue:que forKey:queId];

                i++;
            }
         break;
        }
    }
    [self.table reloadData];
    [self.table reloadInputViews];
  }
 }

编辑2:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
static NSString *CellIdentifier = @"Cell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];

    int rowNumber = indexPath.row;

    cell.textLabel.text = [questions valueForKey:[NSString stringWithFormat:@"%d",rowNumber]];

    NSString *ansCount = [ans_count valueForKey:[NSString stringWithFormat:@"%d",rowNumber]];

    UILabel *ans = [[UILabel alloc] initWithFrame:CGRectMake(15,39,100,12)];
    ans.text =  [NSString stringWithFormat:@"%@%@%@",ansCount,@" ",@"Answers"];
    ans.font = [UIFont fontWithName:@"Arial" size:12.0f] ;
    ans.textColor = [UIColor blueColor];
    [cell.contentView addSubview:ans];

    UIButton *showbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    showbtn.frame = CGRectMake(280, 10, 21, 21);
    showbtn.tag = rowNumber;

    [showbtn setBackgroundImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal];
    [showbtn addTarget:self action:@selector(showDetailedQuestion:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:showbtn];
}

// All bgColor configuration moves here

cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor  = [UIColor grayColor];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16.0f] ;


return cell;
}

3 个答案:

答案 0 :(得分:3)

您有几种选择:

  1. 如果它仅限iPhone或全屏显示,您可以等待viewDidAppear被调用。

  2. 您可以在A上实施refresh来电,然后在viewDidDisappear上的B中,您可以看到B是presentingViewController respondsToSelector:@selector(refresh),如果是,在refresh上致电presentingViewController

  3. 您可以从B发布一些通知并从A中收听。

答案 1 :(得分:2)

使用此方法:

[self dismissViewControllerAnimated:YES completion:^{
                    //Reload the table view here
                }];

(不推荐使用您正在使用的方法。)

答案 2 :(得分:1)

不要在viewDidLoad中调用webservice使用viewWillAppear,因此每次出现视图时都会调用它,你的问题就会消失。

根据你的编辑2:

使用它
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
    }
    int rowNumber = indexPath.row;

    cell.textLabel.text = [questions valueForKey:[NSString stringWithFormat:@"%d",rowNumber]];

    NSString *ansCount = [ans_count valueForKey:[NSString stringWithFormat:@"%d",rowNumber]];

    UILabel *ans = [[UILabel alloc] initWithFrame:CGRectMake(15,39,100,12)];
    ans.text =  [NSString stringWithFormat:@"%@%@%@",ansCount,@" ",@"Answers"];
    ans.font = [UIFont fontWithName:@"Arial" size:12.0f] ;
    ans.textColor = [UIColor blueColor];
    [cell.contentView addSubview:ans];

    UIButton *showbtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    showbtn.frame = CGRectMake(280, 10, 21, 21);
    showbtn.tag = rowNumber;

    [showbtn setBackgroundImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal];
    [showbtn addTarget:self action:@selector(showDetailedQuestion:) forControlEvents:UIControlEventTouchUpInside];

    [cell.contentView addSubview:showbtn];

    // All bgColor configuration moves here

    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor  = [UIColor grayColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16.0f] ;


    return cell;
}