转载自:http://www.w3cschool.cc/ios/att-ios-ui-splitview-htm.html
1 分割视图的使用
2 分割视图是 iPad 的特定视图控制器用于管理两个视图控制器,在左侧是一个主控制器,其右侧是一个详细信息视图控制器。 重要的属性
3
4 delegate
5 viewControllers
6 示例代码和步骤
7 1.创建一个新项目,选择Master Detail Application并单击下一步,输入项目名称,然后选择创建。
8
9 2.简单的分割视图控制器与母版中的表视图是默认创建的。
10
11 3.在这里我们为我们创建的下列文件。
12
13 AppDelegate.h
14 AppDelegate.m
15 DetailViewController.h
16 DetailViewController.m
17 DetailViewController.xib
18 MasterViewController.h
19 MasterViewController.m
20 MasterViewController.xib
21 4. AppDelegate.h文件如下所示
22
23 #import <UIKit/UIKit.h>
24
25 @interface AppDelegate : UIResponder <UIApplicationDelegate>
26
27 @property (strong, nonatomic) UIWindow *window;
28
29 @property (strong, nonatomic) UISplitViewController *splitViewController;
30
31 @end
32 5.在AppDelegate.m中的didFinishLaunchingWithOptions方法,如下所示
33
34 - (BOOL)application:(UIApplication *)application
35 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
36 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]
37 bounds]];
38 // Override point for customization after application launch.
39 MasterViewController *masterViewController = [[MasterViewController
40 alloc] initWithNibName:@"MasterViewController" bundle:nil];
41 UINavigationController *masterNavigationController =
42 [[UINavigationController alloc] initWithRootViewController:
43 masterViewController];
44
45 DetailViewController *detailViewController =
46 [[DetailViewController alloc] initWithNibName:@"DetailViewController"
47 bundle:nil];
48 UINavigationController *detailNavigationController =
49 [[UINavigationController alloc] initWithRootViewController:
50 detailViewController];
51
52 masterViewController.detailViewController = detailViewController;
53
54 self.splitViewController = [[UISplitViewController alloc] init];
55 self.splitViewController.delegate = detailViewController;
56 self.splitViewController.viewControllers =
57 @[masterNavigationController, detailNavigationController];
58 self.window.rootViewController = self.splitViewController;
59 [self.window makeKeyAndVisible];
60 return YES;
61 }
62 6. MasterViewController.h,如下所示
63
64 #import <UIKit/UIKit.h>
65
66 @class DetailViewController;
67
68 @interface MasterViewController : UITableViewController
69
70 @property (strong, nonatomic) DetailViewController *detailViewController;
71
72 @end
73 7. MasterViewController.m,如下所示
74
75 #import "MasterViewController.h"
76
77 #import "DetailViewController.h"
78
79 @interface MasterViewController () {
80 NSMutableArray *_objects;
81 }
82 @end
83
84 @implementation MasterViewController
85
86 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)
87 nibBundleOrNil
88 {
89 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
90 if (self) {
91 self.title = NSLocalizedString(@"Master", @"Master");
92 self.clearsSelectionOnViewWillAppear = NO;
93 self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
94 }
95 return self;
96 }
97
98 - (void)viewDidLoad
99 {
100 [super viewDidLoad];
101 self.navigationItem.leftBarButtonItem = self.editButtonItem;
102
103 UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
104 initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
105 target:self action:@selector(insertNewObject:)];
106 self.navigationItem.rightBarButtonItem = addButton;
107 }
108
109 - (void)didReceiveMemoryWarning
110 {
111 [super didReceiveMemoryWarning];
112 // Dispose of any resources that can be recreated.
113 }
114
115 - (void)insertNewObject:(id)sender
116 {
117 if (!_objects) {
118 _objects = [[NSMutableArray alloc] init];
119 }
120 [_objects insertObject:[NSDate date] atIndex:0];
121 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
122 [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:
123 UITableViewRowAnimationAutomatic];
124 }
125
126 #pragma mark - Table View
127
128 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
129 {
130 return 1;
131 }
132
133 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
134 (NSInteger)section
135 {
136 return _objects.count;
137 }
138
139 // Customize the appearance of table view cells.
140 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
141 (NSIndexPath *)indexPath
142 {
143 static NSString *CellIdentifier = @"Cell";
144
145 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
146 CellIdentifier];
147 if (cell == nil) {
148 cell = [[UITableViewCell alloc] initWithStyle:
149 UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
150 }
151
152
153 NSDate *object = _objects[indexPath.row];
154 cell.textLabel.text = [object description];
155 return cell;
156 }
157
158 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:
159 (NSIndexPath *)indexPath
160 {
161 // Return NO if you do not want the specified item to be editable.
162 return YES;
163 }
164
165 - (void)tableView:(UITableView *)tableView commitEditingStyle:
166 (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:
167 (NSIndexPath *)indexPath
168 {
169 if (editingStyle == UITableViewCellEditingStyleDelete) {
170 [_objects removeObjectAtIndex:indexPath.row];
171 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:
172 UITableViewRowAnimationFade];
173 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
174 // Create a new instance of the appropriate class, insert it into
175 //the array, and add a new row to the table view.
176 }
177 }
178
179 /*
180 // Override to support rearranging the table view.
181 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:
182 (NSIndexPath *) fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
183 {
184 }
185 */
186
187 /*
188 // Override to support conditional rearranging of the table view.
189 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:
190 (NSIndexPath *)indexPath
191 {
192 // Return NO if you do not want the item to be re-orderable.
193 return YES;
194 }
195 */
196
197 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
198 (NSIndexPath *)indexPath
199 {
200 NSDate *object = _objects[indexPath.row];
201 self.detailViewController.detailItem = object;
202 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
203 [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
204 NSString *stringFromDate = [formatter stringFromDate:object];
205 self.detailViewController.detailDescriptionLabel.text = stringFromDate;
206 }
207
208 @end
209
210 8. DetailViewController.h ,如下所示
211
212 #import <UIKit/UIKit.h>
213
214 @interface DetailViewController : UIViewController
215 <UISplitViewControllerDelegate>
216
217 @property (strong, nonatomic) id detailItem;
218
219 @property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
220 @end
221 9. DetailViewController.m ,如下所示
222
223 #import "DetailViewController.h"
224
225 @interface DetailViewController ()
226 @property (strong, nonatomic) UIPopoverController *masterPopoverController;
227 - (void)configureView;
228 @end
229
230 @implementation DetailViewController
231
232 #pragma mark - Managing the detail item
233
234 - (void)setDetailItem:(id)newDetailItem
235 {
236 if (_detailItem != newDetailItem) {
237 _detailItem = newDetailItem;
238
239 // Update the view.
240 [self configureView];
241 }
242
243 if (self.masterPopoverController != nil) {
244 [self.masterPopoverController dismissPopoverAnimated:YES];
245 }
246 }
247
248 - (void)configureView
249 {
250 // Update the user interface for the detail item.
251
252 if (self.detailItem) {
253 self.detailDescriptionLabel.text = [self.detailItem description];
254 }
255 }
256
257 - (void)viewDidLoad
258 {
259 [super viewDidLoad];
260 [self configureView];
261 }
262
263 - (void)didReceiveMemoryWarning
264 {
265 [super didReceiveMemoryWarning];
266 // Dispose of any resources that can be recreated.
267 }
268
269 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:
270 (NSBundle *)nibBundleOrNil
271 {
272 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
273 if (self) {
274 self.title = NSLocalizedString(@"Detail", @"Detail");
275 }
276 return self;
277 }
278
279 #pragma mark - Split view
280
281 - (void)splitViewController:(UISplitViewController *)splitController
282 willHideViewController:(UIViewController *)viewController withBarButtonItem:
283 (UIBarButtonItem *)barButtonItem forPopoverController:
284 (UIPopoverController *)popoverController
285 {
286 barButtonItem.title = NSLocalizedString(@"Master", @"Master");
287 [self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
288 self.masterPopoverController = popoverController;
289 }
290
291 - (void)splitViewController:(UISplitViewController *)splitController
292 willShowViewController:(UIViewController *)viewController
293 invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
294 {
295 // Called when the view is shown again in the split view,
296 //invalidating the button and popover controller.
297 [self.navigationItem setLeftBarButtonItem:nil animated:YES];
298 self.masterPopoverController = nil;
299 }
300
301 @end
302 10.现在当我们运行应用程序时,在横向模式下我们会得到下面的输出
303
304 masterDetailOutput1
305
306 11. 当我们切换到纵向模式,我们会获得下面的输出:
307
308 masterDetailOutput2