iOS:在类型对象上找不到属性

时间:2017-01-12 14:27:36

标签: ios objective-c timer xcode7

// First VC prepareForSegue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"your segue"])
    {
        SecondVC *vc = (SecondVC *)segue.destinationViewController;
       [SecondVC.timer startTimer:nil];
    }
}

// Create Property of  NSTimer *timer
// Second VC's Public Methods
@interface SecondVC ()
{
   NSTimer *_timer;
}

- (void)_timerFired:(NSTimer *)timer;
@end

@implementation SecondVC

- (IBAction)startTimer:(id)sender {
    if (!_timer) {
     _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                      target:self
                                    selector:@selector(_timerFired:)
                                    userInfo:nil
                                     repeats:YES];
     }
 }

- (void)_timerFired:(NSTimer *)timer {
     NSLog(@"Timer fired");
}

嗨,我想点击viewController(A)中的按钮,这将在viewController(B)中触发一个计时器。这是我到目前为止的代码,但是我从这行"Property 'timer' not found on object of type secondVC "继续得到[SecondVC.timer startTimer:nil];。谢谢,我已经检查了viewController(A)的import语句是在viewController(B)中。如何解决此错误,谢谢?

5 个答案:

答案 0 :(得分:0)

尝试使用[vc startTimer:nil];代替[SecondVC.timer startTimer:nil];,并将startTimer:方法添加到SecondVC界面:

@interface SecondVC ()
{
   NSTimer *_timer;
   - (IBAction)startTimer:(id)sender;
}

答案 1 :(得分:0)

您未使用UIViewController对象访问计时器,请使用[vc startTimer:nil];代替[SecondVC.timer startTimer:nil]

确保将- (IBAction)startTimer:(id)sender放在标题(.h)文件中。

答案 2 :(得分:0)

       // First VC prepareForSegue
 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
        {
             if([segue.identifier isEqualToString:@"your segue"])
             {
                SecondVC *vc = (SecondVC *)segue.destinationViewController;
                 [vc.timer startTimer:nil];
             }
        }

        //Add below line in  second Vc 's .h file.
    @property (nonatomic, strong) NSTimer *timer;
     - (void)stopTimer:(id)sender; 
    - (void)timerFired:(NSTimer *)timer; 

        // Second VC's Public Methods
        @interface SecondVC ()
        @end

        @implementation SecondVC

        - (void)startTimer:(id)sender {
            if (!self.timer) {
                self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                          target:self
                                                        selector:@selector(timerFired:)
                                                        userInfo:nil
                                                         repeats:YES];
            }
        }

        - (IBAction)stopTimer:(id)sender {
            if ([self.timer isValid]) {
                [self.timer invalidate];
            }
            self.timer = nil;
        }

        - (void)timerFired:(NSTimer *)timer {
            NSLog(@"Timer fired");
        }

答案 3 :(得分:-1)

首先,您需要在IBAction

接口(.h)中声明SecondVC
- (IBAction)startTimer:(id)sender;

然后在实例上调用方法,而不是类型

[vc startTimer:nil];

答案 4 :(得分:-1)

所以我认为你的计时器不是一个可见的属性。尝试设置

-(void)startTimer;
<\ n>在SecondViewController.h中; 然后在FirstVc中使用。

 SecondVC *vc = (SecondVC *)segue.destinationViewController;
 [vc startTimer];

在SceondVc中写下这个:

-(void)startTimer{
    _timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(_timerFired:) userInfo:nil repeats:YES];
}