得到错误“Unknown receiver coachMarksView;你的意思是WSCoachMarksView错误吗?”

时间:2014-11-20 19:54:24

标签: ios objective-c

我目前正在实施WSCoachMarksView框架,以便在首次使用该应用时向用户介绍功能。但是,viewDidAppear中的代码会出现以下错误:Unknown receiver 'coachMarksView'; Did you mean 'WSCoachMarksView'?

我不确定为什么会发生这种情况,因为我已经在coachMarksView中实例化viewDidLoad,所以它应该识别它。我错过了什么吗?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Setup coach marks
    NSArray *coachMarks = @[
                            @{
                                @"rect": [NSValue valueWithCGRect:(CGRect){{50,168},{220,45}}],
                                @"caption": @"Just browsing? We'll only notify you periodically of new matches. Need it soon? We'll notify you more frequently, and match you with items that are closer to you."
                                },
                            ];

    WSCoachMarksView *coachMarksView = [[WSCoachMarksView alloc] initWithFrame:self.navigationController.view.bounds coachMarks:coachMarks];
    [self.navigationController.view addSubview:coachMarksView];
    coachMarksView.animationDuration = 0.5f;
    coachMarksView.enableContinueLabel = YES;
    [coachMarksView start];
}

- (void)viewDidAppear:(BOOL)animated
{

    // Show coach marks
    BOOL coachMarksShown = [[NSUserDefaults standardUserDefaults] boolForKey:@"WSCoachMarksShown"];
    if (coachMarksShown == NO) {
        // Don't show again
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"WSCoachMarksShown"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        // Show coach marks
        [coachMarksView start];

        // Or show coach marks after a second delay
        // [coachMarksView performSelector:@selector(start) withObject:nil afterDelay:1.0f];
    }   
}

2 个答案:

答案 0 :(得分:1)

您已将coachMarksView声明为viewDidLoad内的局部变量。 “本地”表示它仅在您声明它的位置可见。

尝试将其更改为类的属性,以便您的对象可以从其所有方法访问它。 (使用self.coachMarksView。)

答案 1 :(得分:1)

您需要将coachMarksView设为属性,以便您可以访问同一个实例。在viewWillAppear中未定义coachMarksView:因为该范围不了解viewDidLoad中的范围。

要为coachMarksView创建属性,您需要在viewController中执行以下操作:

@interface UIViewController ()
@property (nonatomic, strong) WKCoachMarksView *coachMarksView;
@end

然后在viewDidLoad

- (void)viewDidLoad
{
  self.coachMarksView = [[WSCoachMarksView alloc] initWithFrame:self.navigationController.bounds]];
}

现在访问该实例只需使用self.coachMarksView。

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];
  [self.coachMarksView start];
}

以下是有关Objective-C http://rypress.com/tutorials/objective-c/properties.html

中的getter,setter和properties的更多信息