属性需要定义方法

时间:2013-02-11 06:52:23

标签: ios objective-c

我正在尝试创建一个SplitViewController视图但是我收到以下警告:

属性splitViewController需要定义setSplitViewController方法-use @ Synthesize,@ dynamic或在此类实现中提供方法实现。

以下是代码

///AppDelegate.h

 @class ViewController;
 @class DetailViewController;

 @interface AppDelegate : UIResponder <UIApplicationDelegate, UISplitViewControllerDelegate>
{
UISplitViewController *splitViewController;
ViewController *viewcontroller;
DetailViewController *detailViewController;
}
@property (nonatomic,retain) UIWindow *window;
@property (nonatomic,retain) DetailViewController *detailViewController;
@property(nonatomic,retain)  UISplitViewController *splitViewController;
@property (nonatomic,retain) ViewController *viewController;

@end

///AppDelegate.m"

#import "ViewController.h"
#import "DetailViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;
 @synthesize splitviewController;
 @synthesize detailViewController;
- (void)dealloc
{
   [_window release];
   [_viewController release];
   [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   ViewController *rootViewController = [[ViewController alloc]   initWithStyle:UITableViewStylePlain];
   UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
   detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
   rootViewController.detailViewController = detailViewController;    

   splitViewController = [[UISplitViewController alloc] init];
   splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];
   splitViewController.delegate = detailViewController;

   [self.window makeKeyAndVisible];
   return YES;
}




///ViewController.h
 #import <UIKit/UIKit.h>
 @class DetailViewController;
 @interface ViewController : UITableViewController{
 DetailViewController *detailViewController;
 NSMutableArray *phone;
 }
@property (nonatomic,retain)IBOutlet DetailViewController *detailViewController;
@property (nonatomic,retain) NSMutableArray *phone;
@end



///ViewController.m
#import "ViewController.h"
#import "DetailViewController.h"

@interface ViewController ()

@end

 @implementation ViewController
 @synthesize detailViewController,phone;

  - (CGSize)contentSizeForViewInPopoverView {
 return CGSizeMake(320, 600);
 }


- (void)viewDidLoad
{
  [super viewDidLoad];
  self.phone = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"phone" ofType:@"plist"]] retain];

// Do any additional setup after loading the view, typically from a nib.
  }

  - (void)viewDidUnload
{
 [super viewDidUnload];
// Release any retained subviews of the main view.
 }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
 return YES;
 }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {
  // Return the number of sections.
 return 1;
 }


 - (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [phone count];
 }


  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *CellIdentifier = @"CellIdentifier";

// Dequeue or create a cell of the appropriate type.
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.accessoryType = UITableViewCellAccessoryNone;
 }

// Configure the cell.
 cell.textLabel.text = [self.phone objectAtIndex:indexPath.row];
 return cell;
 }
  - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  /*
   When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
   */
 detailViewController.detailItem = [self.phone objectAtIndex:indexPath.row];
 }
 - (void)dealloc {
 [detailViewController release];
 [super dealloc];
 }



  @end

3 个答案:

答案 0 :(得分:1)

问题是你的@synthesize语句中拼写错误的splitViewController - 你没有把v大写。

如果您以简单的方式完成此操作,则不会遇到此问题。不再需要实例变量或@synthesize语句 - 在创建属性时自动获得两者。

答案 1 :(得分:0)

在您的情况下,添加@synthesize splitViewController = _splitViewController; detailViewController = _ detailViewController;

以下是有用的代码,如何添加UISplitViewController

/// AppDelegate.h file

#import <UIKit/UIKit.h>
#import "MasterViewController.h"
#import "DetailViewController.h"


@interface AppDelegate : UIResponder <UIApplicationDelegate, UISplitViewControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic, strong)  UISplitViewController *splitViewController;
@property (nonatomic, strong)  MasterViewController *masterVC;
@property (nonatomic, strong)  DetailViewController *detailVC;

@property (nonatomic, strong)  UINavigationController *mvcNavCon;
@property (nonatomic, strong)  UINavigationController *dvcNavCon;

@end


/// AppDelegate.m File

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    self.masterVC = [[MasterViewController alloc] init];
    self.mvcNavCon = [[UINavigationController alloc] initWithRootViewController:self.masterVC];

    self.detailVC = [[DetailViewController alloc] init];
    self.dvcNavCon = [[UINavigationController alloc] initWithRootViewController:self.detailVC];

    self.splitViewController = [[UISplitViewController alloc] init];
    self.splitViewController.delegate = self;
    self.splitViewController.viewControllers = [NSArray arrayWithObjects:self.mvcNavCon, self.dvcNavCon,nil];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.splitViewController;

    [self.window makeKeyAndVisible];
    return YES;
}

答案 2 :(得分:0)

在您的密码中,您还没有合成您的splitViewController媒体资源。由于您尚未将其合成为属性,因此编译器会发出警告,要求您合成属性,以便它可以自动为您的信任生成setter和getter(您可以使用生成的setter和getter使用.表示法如在self.splitViewController中那样将其合成为  @synthesize splitViewController = _splitViewController

将您自己的自定义setter和getter实现为

//setter
- (void)setSplitViewController:(UISplitViewController*)splitViewController_ {
    //assuming your property has retain identifier
    if (splitViewController != splitViewController_) {
        [splitViewController release];
        splitViewController = [splitViewController_ retain];
    }
}

//getter
- (UISplitViewController*)splitViewController {
    return splitViewController;
}

使用@dynamic splitViewController将属性声明为动态。这意味着该属性的setter和getter将从其他地方提供。

修改

使用以下内容替换appDelegate.m中的didFinishLaunchingWithOptions方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   _viewController = [[ViewController alloc] initWithNibName:@"ViewController's nib name" bundle:nil];
   UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
   detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
   rootViewController.detailViewController = detailViewController;    

   splitViewController = [[UISplitViewController alloc] init];
   splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];
   splitViewController.delegate = detailViewController;

   self.window.rootViewController = splitViewController;
   return YES;
}

还编辑dealloc:

- (void)dealloc
{
   [_window release];
   [_viewController release];
   [splitViewController release];
   [detailViewController release];
   [super dealloc];
}

在viewController viewDidLoad中将self.phones替换为

  

self.phone = [[NSArray arrayWithObjects:@&#34; Cell ONE&#34;,@&#34; Cell TWO&#34;,@&#34; Cell THREE&#34;,@&#34; Cell FOUR&#34;,@&#34; Cell FIVE&#34;,@&#34; Cell SIX&#34;,nil];

这仅用于测试阵列部件是否正确加载..因此,如果要创建单元格,您可以看到它们。在cellForRowAtIndexPath方法中设置一个断点并查看它是否被称为

然后最后在didSelect看看detailItem iVar是不是nil。

是的,在加载之前检查NIB名称,并确保NIB中的所有插座都正确连接。

干杯,玩得开心。