【问题标题】:I get the error Property '...' not found on object of type '...'我收到错误属性“...”未在“...”类型的对象上找到
【发布时间】:2013-07-09 01:35:45
【问题描述】:

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:        
goes on like normal app..........
@end

FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *bGround;

- (IBAction)settingsPressed:(id)sender;

- (IBAction)startPressed:(id)sender;

@end


@class SecondViewController;

@interface SecondViewController : UIViewController


@property(strong,nonatomic)SecondViewController *secondViewController;

@end

FirstViewController.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)settingsPressed:(id)sender {

在这一行上,它显示“在 'FirstViewController' 类型的对象上找不到属性 'secondViewController' 这是下面的行。

self.secondViewController =

[[SecondViewController alloc] initWithNibName:@"SecondViewController"
                                       bundle:nil];

它与下面一行的最后一个警告相同。

[self presentViewController:self.secondViewController animated:YES completion:nil];

}


- (IBAction)startPressed:(id)sender {
}
@end

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@end

SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

  • 您从未将 secondViewController 声明为 FirstViewController 中的属性。
  • 这家伙根本没有合成属性 secondviewController

标签: ios objective-c properties


【解决方案1】:

我对您为什么要声明 SecondViewController 两次感到困惑。您似乎在 FirstViewControler.h 和它自己的 .h 文件中都这样做。无论如何,您的问题似乎是您已为 SecondViewController 提供了您尝试访问的属性。重读自己的代码:

@interface SecondViewController : UIViewController


@property(strong,nonatomic)SecondViewController *secondViewController;

@end

你想要的 FirstViewController.h 文件是这样的:

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *bGround;
@property(strong,nonatomic)SecondViewController *secondViewController;

- (IBAction)settingsPressed:(id)sender;

- (IBAction)startPressed:(id)sender;

@end

请注意,我在 .h 文件的顶部导入 SecondViewController,并在其@interface 中声明该属性。

【讨论】:

    猜你喜欢
    • 2012-12-10
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2014-04-27
    • 2012-03-04
    • 2022-12-02
    相关资源
    最近更新 更多