iOS目标c执行选择器不执行该方法

时间:2017-06-02 04:21:58

标签: ios objective-c xcode segue

实际上,我在url请求块中使用了执行选择器,它一开始工作正常,但现在它没有执行函数

执行选择器功能

       NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
        NSLog(@"requestReply: %@", jsonDict);
        self.tmp=[jsonDict valueForKey:@"otp"] ;
        self.str=self.tmp;
        NSLog(@"tmp storage inside block:%@",self.tmp);
      //  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateStatus) userInfo:nil repeats:YES];
        [self performSelector:@selector(updateStatus) withObject:self afterDelay:1.0];
    }] resume];
}

并且更新状态方法是

-(void)updateStatus{
NSLog(@" storage:%@",self.str);
NSLog(@"tmp storage:%@",self.tmp);
[ self performSegueWithIdentifier:@"b1" sender:self];
}

因为它没有执行我无法执行segue并且应用程序停留在同一页面

4 个答案:

答案 0 :(得分:2)

简单,在主线程中更新您的UI

  dispatch_async(dispatch_get_main_queue(), ^{
        [self performSelector:@selector(updateStatus) withObject:nil afterDelay:0.1];
         });

选项2

或创建简单方法

       NSLog(@"tmp storage inside block:%@",self.tmp);
        [self updateStatus];

并在主线程中调用方法

-(void)updateStatus{
NSLog(@" storage:%@",self.str);
NSLog(@"tmp storage:%@",self.tmp);
dispatch_async(dispatch_get_main_queue(), ^{
    [ self performSegueWithIdentifier:@"b1" sender:self];
});
}

更新代码

 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
       // NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is json string
        //   NSError *error;
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
        NSLog(@"requestReply: %@", jsonDict);
        self.tmp=[jsonDict valueForKey:@"otp"] ;
        self.str=self.tmp;
        NSLog(@"tmp storage inside block:%@",self.tmp);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self performSelector:@selector(updateStatus) withObject:nil afterDelay:0.1];
        });

    }] resume];

并将方法调用为

 -(void)updateStatus{
NSLog(@" storage:%@",self.str);
NSLog(@"tmp storage:%@",self.tmp);
[ self performSegueWithIdentifier:@"b1" sender:self];


}

答案 1 :(得分:0)

如果从后台线程调用

performSelector将不起作用。试试这个......

dispatch_async(dispatch_get_main_queue(), ^(void){
    //Run UI Updates
    [self performSelector:@selector(updateStatus) withObject:nil afterDelay:0.1];
});

答案 2 :(得分:0)

尝试[self performSelector:@selector(updateStatus) withObject:nil afterDelay:1.0];

selector没有参数,withobject是SEL的参数,因此对象应为零。

答案 3 :(得分:-1)

您必须传入响应指定选择器的相关对象。传递nil会向nil发送消息并且不执行任何操作。 假设updateStatus方法在您调用performSelector的同一个类中声明,您可以这样做:

[self performSelector:@selector(updateStatus) withObject:self afterDelay:0.1];