1

我正在展示一个视图说 B 使用

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

从这个视图中,我关闭它以返回查看 A 使用

  [self dismissModalViewControllerAnimated:YES];

现在,当我回去时,我想调用重新加载上一个视图(此处为视图 A)。视图 A 是具有来自服务器的数据列表的表视图。所以 reloadtable 不起作用,因为新数据只能来自服务器。所以简而言之,我想在关闭视图 B 时调用该服务器并刷新该视图。

这个怎么做。?

我曾尝试在视图 A 的 ViewWillAppear 方法中调用 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;
}
4

3 回答 3

3

你有几个选择:

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

  2. 您可以在 A 上实现refresh调用,然后在 B 中,在 上viewDidDisappear,您可以查看 B 是否存在,如果是,则在 上presentingViewController respondsToSelector:@selector(refresh)调用。refreshpresentingViewController

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

于 2013-09-06T04:35:08.223 回答
2

使用此方法:

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

(您使用的方法已弃用。)

于 2013-09-06T04:31:00.007 回答
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;
}
于 2013-09-06T04:36:32.510 回答