在didSelectRowAtIndexPath之前调用PrepareForSegue

时间:2016-09-21 12:07:34

标签: ios objective-c segue

我在didSelectRowAtIndexPath中调用以下方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:    (NSIndexPath *)indexPath{
[self unreadMessageCounter];
}

在该方法中,我得到一个参数名为“MsgCount”的值。为此,书面代码是。

-(void) unreadMessageCounter{
NSUserDefaults *defaultUser=[NSUserDefaults standardUserDefaults];
NSString* username = [defaultUser objectForKey:KUserName];
NSString* password = [defaultUser objectForKey:KPassword];
dispatch_async(dispatch_get_main_queue(), ^{
    [SVProgressHUD showWithStatus:@"Loading..."       maskType:SVProgressHUDMaskTypeGradient];
});

NSString *url3 ;
NSString *base_url=[[NSUserDefaults standardUserDefaults]     objectForKey:@"BASE_URL"];
url3=[[NSString alloc]initWithFormat:@"%@%@?    username=%@&password=%@&deviceUniqueId=%@",base_url,MESSAGE_COUNTER,username,password,[defaultUser objectForKey:KDeviceToken]];
[defaultUser synchronize];


NSURL *requestURL =  [NSURL URLWithString:[url3 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:requestURL];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *urlResponse, NSError *error) {
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)urlResponse;
    NSLog(@"Response Code For Message Counter:: %ld", (long)[response statusCode]);
    if(response){
        NSMutableDictionary *returneDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSLog(@"Return Dict For Message Counter:: %@", returneDict);

        if (returneDict != nil) {
            if ([returneDict valueForKey:@"valueSet"]){
                for (NSDictionary *dict in [returneDict valueForKey:@"valueList"]) {
                    _counterNumber = dict[@"MsgCount"];
                    NSLog(@"counter number %@", _counterNumber);
                }
            }
            else{
                dispatch_async(dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
            }
        }
        else{
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        }
    }
}];
[task resume];

}

之后我在prepareForSegue的帮助下将_counterNumber字符串传递给下一个视图控制器,因为以下代码是。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if (![segue.identifier isEqualToString:@"Show Notification"]) {
    UINavigationController *nav = [segue destinationViewController];
    WelcomeScreenViewController *welcomeScreenViewController =     (WelcomeScreenViewController *)nav.topViewController;
    welcomeScreenViewController.counterString = _counterNumber;
  }
}

它会进入下一个视图控制器,但是在第一次调用之后,在didselectrowatindexpath而不是unreadMessageCounter方法之后,prepareForSegue正在调用,这就是为什么_counterNumber值我得到的nil,但是在我第二次调用时它是像往常一样工作。在这种情况下,请帮助我,因为我没有得到任何线索。

1 个答案:

答案 0 :(得分:1)

首先将segue从单元格与nextViewController断开连接。并将当前ViewController中的新segue设置为nextViewController,如下面的屏幕截图enter image description here

处理完您在didSelectRowAtIndexPath中调用的API后,通过以下代码执行segue。

[self performSegueWithIdentifier: @"Show Notification" sender: self];