`
jsntghf
  • 浏览: 2534072 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

UISplitView的简单示例

    博客分类:
  • iOS
阅读更多

splitAppDelegate.h

 

#import <UIKit/UIKit.h>

@class MasterViewController;
@class DetailViewController;

@interface splitAppDelegate : NSObject <UIApplicationDelegate> {    
    UIWindow *window;    
    UISplitViewController *splitViewController;    
    MasterViewController *masterViewController;
    DetailViewController *detailViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UISplitViewController *splitViewController;
@property (nonatomic, retain) MasterViewController *masterViewController;
@property (nonatomic, retain) DetailViewController *detailViewController;

@end

 

splitAppDelegate.m

 

#import "splitAppDelegate.h"
#import "MasterViewController.h"
#import "DetailViewController.h"

@implementation splitAppDelegate

@synthesize window, splitViewController, masterViewController, detailViewController;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
	masterViewController = [[MasterViewController alloc] initWithStyle:UITableViewStylePlain];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
	
    detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
    masterViewController.detailViewController = detailViewController;
    
    splitViewController = [[UISplitViewController alloc] init];
    splitViewController.viewControllers = [NSArray arrayWithObjects:navigationController, detailViewController, nil];
	splitViewController.delegate = detailViewController;
	
	[masterViewController release];
	[detailViewController release];
	[navigationController release];
    
	[window addSubview:splitViewController.view];
    [window makeKeyAndVisible];
    
    return YES;
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [splitViewController release];
    [window release];
    [super dealloc];
}

@end

 

MasterViewController.h

 

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface MasterViewController : UITableViewController {
    DetailViewController *detailViewController;
	NSMutableArray * fruits;
}

@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
@property (nonatomic, retain) NSMutableArray *fruits;

@end

 

MasterViewController.m

 

#import "MasterViewController.h"
#import "DetailViewController.h"

@implementation MasterViewController

@synthesize detailViewController;
@synthesize fruits;

#pragma mark -
#pragma mark Size for popover

- (CGSize)contentSizeForViewInPopoverView {
    return CGSizeMake(200.0, 400.0);
}

#pragma mark -
#pragma mark View lifecycle

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

#pragma mark -
#pragma mark Table view data source

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
	return [fruits count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
	static NSString *CellIdentifier = @"CellIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.textLabel.text = [self.fruits objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	detailViewController.detailItem = [self.fruits objectAtIndex: indexPath.row];
}

#pragma mark -
#pragma mark Memory management

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

@end

 

DetailViewController.h

 

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UISplitViewControllerDelegate> {    
    UIPopoverController *popoverController;
    UINavigationBar *navigationBar;    
    id detailItem;	
	IBOutlet UIImageView *fruitImageView;
}

@property (nonatomic, retain) UIPopoverController *popoverController;
@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;
@property (nonatomic, retain) id detailItem;
@property (nonatomic, retain) IBOutlet UIImageView *fruitImageView;

@end

 

DetailViewController.m

 

#import "DetailViewController.h"
#import "MasterViewController.h"

@implementation DetailViewController

@synthesize navigationBar, popoverController, detailItem,fruitImageView;

#pragma mark -
#pragma mark Managing the popover controller

- (void)setDetailItem:(id)newDetailItem {
    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];
		
        navigationBar.topItem.title = detailItem;
		NSString * imageName = [NSString stringWithFormat:@"%@.png",detailItem];
		[self.fruitImageView setImage:[UIImage imageNamed:imageName]];
    }
	
    if (popoverController != nil) {
        [popoverController dismissPopoverAnimated:YES];
    }        
}

#pragma mark -
#pragma mark Split view support

- (void)splitViewController: (UISplitViewController*)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem*)barButtonItem forPopoverController: (UIPopoverController*)pc {    
    barButtonItem.title = @"Master List";
    [navigationBar.topItem setLeftBarButtonItem:barButtonItem animated:YES];
    self.popoverController = pc;
}

- (void)splitViewController: (UISplitViewController*)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {    
	self.popoverController = nil;
}

- (void)viewDidUnload {
	self.popoverController = nil;
}

#pragma mark -
#pragma mark Memory management

- (void)dealloc {
    [popoverController release];
    [navigationBar release];    
    [detailItem release];
    [super dealloc];
}

@end

 

示例图:


分享到:
评论
4 楼 itliugang 2013-07-16  
博主,能把代码提供下,参考吗?灰常感谢!
3 楼 aceor 2012-07-17  
楼主,你能把你的程序发给我看看吗?邮箱地址:94520491@qq.com。非常感谢。
2 楼 jsntghf 2012-07-17  
DetailViewController在xib里是有的,按照本文的做法是可以实现的。
1 楼 aceor 2012-07-17  
楼主,这个程序中MasterViewController.h文件
@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
这个iboutlet是怎么关联的?你的DetailViewController是在splitAppDelegate.m中程序动态生成的,不是xib文件画的,这个怎么关联呢?求指点。
另外博主可不可以把这个程序的代码发给我参考一下啊。

相关推荐

Global site tag (gtag.js) - Google Analytics