1

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

属性 splitViewController 需要定义方法 setSplitViewController - 使用 @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
4

3 回答 3

1

在您的代码中,您尚未合成您的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 将从其他地方提供。

编辑:

didFinishLaunchingWithOptions将appDelegate.m 中的方法替换为以下内容:

- (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];
}

并在 viewControllerviewDidLoad中用这个替换 self.phones 行

self.phone = [[NSArray arrayWithObjects:@"细胞一",@"细胞二",@"细胞三",@"细胞四",@"细胞五",@"细胞六", nil];

这只是为了测试数组部分是否正确加载..以便您可以看到正在创建的单元格。在方法中放置一个断点cellForRowAtIndexPath,看看它是否被调用

然后最后didSelect看看detailItem iVar 是否不为零。

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

干杯,玩得开心。

于 2013-02-11T07:13:21.723 回答
1

问题是你在 @synthesize 语句中拼错了 splitViewController——你没有大写 v。

如果你用简单的方法来做,你就不会遇到这个问题。不再需要实例变量或@synthesize 语句——创建属性时您会自动获得这两者。

于 2013-02-11T07:45:59.640 回答
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;
}
于 2013-02-11T06:54:13.227 回答