开始一个基于导航的应用
一个关于如何创建基于导航的应用程序和视图之间过渡的简单教程。
引言
我开发 iPhone 应用的时间不长,但希望这个简单的教程能让你清楚地了解如何开始
- 创建基于导航的应用
- 创建新的类对象
- 用对象数组填充
UITableView
- 视图之间的过渡
第一步:创建新项目
启动 X-Code 并选择 文件 > 新建项目 > 基于导航的应用程序 > 选择。
给项目命名为 'MusicApp'。
第二步:创建我们的对象
下一步是创建一个继承自 NSObject
的 'Artist
' 类。我们将用它来创建可以在应用的第一屏幕上由用户选择的对象。
点击 文件 > 新建文件 > Objective-C 类,然后选择 NSObject
的子类。
将类命名为 'Artist
',并确保 Artist.h 被选中。现在我们需要定义这个类的属性。在这个例子中,一个艺术家将有一个名字和一段传记。
将以下代码添加到 Artist.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface Artist : NSObject {
NSString *name;
NSString *biography;
}
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *biography;
- (id)initWithName:(NSString*)n biography:(NSString *)bio;
@end
行 -(id)initWithName:(NSString*)n description:(NSString *)desc;
是一个函数的声明,用于初始化我们的 Artist
对象。所有 NSObject
都有一个 init
方法,但我们会创建自己的方法,以便传递额外的参数来初始化对象。
将以下代码添加到 Artist.m
#import "Artist.h"
@implementation Artist
@synthesize name,biography;
- (id)initWithName: (NSString*)n biography:(NSString*)bio {
self.name = n;
self.biography = bio;
return self;
}
@end
上面,我们实现了 initWithName
方法,该方法接受两个参数并将它们分配给我们的局部变量,就像任何其他构造函数一样。return self;
行是返回对象的新实例所必需的。
第三步:视图之间的过渡
我们要做的第一件事是设置第一个屏幕的标题。这是创建后退按钮所必需的。
将以下代码添加到 RootViewController.m
#import "RootViewController.h"
#import "MusicAppAppDelegate.h"
#import "Artist.h"
@implementation RootViewController
@synthesize artistView;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Artists";
}
viewDidLoad
方法用于设置第一个屏幕的标题。您会注意到我们添加了 'artistView
'。这是第二个 ViewController,我们稍后会创建它(当用户选择一个艺术家时,我们会显示这个视图)。@synthesize artistView;
行用于添加 getter 和 setter 方法。
我们现在将声明一个艺术家对象数组,可以在第一个屏幕上选择。
将以下代码添加到 MusicAppAppDelegate.h
#import <uikit>
@interface MusicAppAppDelegate : NSObject <uiapplicationdelegate> {
UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *artists;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet
UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *artists;
声明数组后,我们现在需要创建我们的 Artist
对象。
将以下代码添加到 MusicAppAppDelegate.m
#import "MusicAppAppDelegate.h"
#import "RootViewController.h"
#import "Artist.h"
@implementation MusicAppAppDelegate
@synthesize window;
@synthesize navigationController;
@synthesize artists;
#pragma mark -
#pragma mark Application lifecycle
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
Artist *artist1 = [[Artist alloc] initWithName:@"Artist1" biography:@"Type bio here"];
Artist *artist2 = [[Artist alloc] initWithName:@"Artist2" biography:@"Artist 2 biogrpahy"];
Artist *artist3 = [[Artist alloc] initWithName:@"Artist3" biography:@"Artist 3 biography"];
self.artists = [[NSMutableArray alloc] initWithObjects:artist1,artist2,artist3,nil];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
首先要注意的是,我们在顶部添加了 @synthesize artists;
。这样其他对象就可以访问我们声明的数组了。
在 applicationDidFinishLaunching
方法中,我们使用之前编写的构造函数创建了三个 Artist
对象。行 self.artists = [[NSMutableArray alloc] initWithObjects:artist1,artist2,artist3,nil];
创建了一个包含我们 Artist
对象的新数组,并将其分配给我们声明的数组。请注意,nil
是最后一个参数,是结束数组所必需的。
当用户在第一个屏幕上选择一个艺术家时,我们想在第二个屏幕上显示他们的传记。创建一个新的 ViewController,命名为 'ArtistViewController
' - 文件 > 新建文件。
确保选中了 '使用 XIB 作为用户界面'。XIB 文件是呈现给用户的界面。双击 XIB 文件,Interface Builder 将会加载。将一个 UITextView
拖到视图上;这将用于显示艺术家的传记。保存并关闭 Interface Builder。
在 ArtistViewController.h 中,添加以下代码
#import <uikit>
@interface ArtistViewController : UIViewController {
IBOutlet UITextView *artistBio;
}
@property(nonatomic,retain) IBOutlet UITextView *artistBio;
@end
在这里,我们创建了一个 Interface Builder Outlet,以便我们可以将一个属性与 UITextView
关联并在屏幕上显示它。在 ArtistViewController.m 的 @implementation
下添加行 @synthesize artistBio;
,以创建 artistBio
属性的 getter 和 setter 方法。
我们现在需要将 outlet 连接到 UITextView
。为此,请再次双击 ArtistViewController.xib 文件以打开 Interface Builder。点击 **文件所有者**,然后转到 **工具 > 连接检查器**。只需将 outlet 拖到 UITextView
上,然后保存并关闭 Interface Builder。
为了过渡到这个视图,我们需要在初始 ViewController
中声明一个变量。
将以下代码添加到 RootViewController.h
#import "ArtistViewController.h"
@interface RootViewController : UITableViewController {
ArtistViewController *artistView;
}
@property(nonatomic,retain)ArtistViewController *artistView;
@end
通过添加行 #import "ArtistViewController.h"
,我们可以创建 ArtistViewController
的新实例。
第四步:显示和选择对象
为了告诉第一个 ViewController 要显示多少个项目,有一个名为 numberOfRowsInSection
的方法。我们需要将这个数字设置为我们之前声明的艺术家数组的大小。
打开 RootViewController.m,并添加以下代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
MusicAppAppDelegate *appDelegate = (
MusicAppAppDelegate *)[[UIApplication sharedApplication] delegate];
return appDelegate.artists.count;
}
第一行让我们能够访问我们声明了艺术家数组的 appDelegate
。然后我们只需要返回数组的项目数。
接下来,我们需要在第一个视图上显示艺术家的名字。将以下代码添加到 cellForRowAtIndexPath
方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(
NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell =
[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
MusicAppAppDelegate *appDelegate =
(MusicAppAppDelegate *) [[UIApplication sharedApplication] delegate];
Artist *artist = (Artist *)[appDelegate.artists objectAtIndex:indexPath.row];
[cell setText:artist.name];
return cell;
}
我们添加的第一行用于再次访问存储数组的 appDelegate
。然后我们用当前单元格索引(indexPath.row
)索引艺术家数组以返回一个 Artist
。然后将单元格的 Text
属性设置为艺术家的名字。
最后一步是确定用户选择了哪个艺术家。在 didSelectRow
方法中,添加以下代码
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here -- for example, create and push another view controller.
// AnotherViewController *anotherViewController =
// [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
// [self.navigationController pushViewController:anotherViewController animated:YES];
// [anotherViewController release];
MusicAppAppDelegate *appDelegate = (
MusicAppAppDelegate *)[[UIApplication sharedApplication] delegate];
Artist *artist = (Artist *)[appDelegate.artists objectAtIndex:indexPath.row];
if(self.artistView == nil){
ArtistViewController *viewController =
[[ArtistViewController alloc] initWithNibName:@"ArtistViewController"
bundle:[NSBundle mainBundle]];
self.artistView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.artistView animated:YES];
self.artistView.title = [artist name];
self.artistView.artistBio.text = [artist biography];
}
与前一个方法一样,我们从 appDelegate
访问艺术家数组,并使用选定的单元格索引(indexPath.row
)索引该数组以返回一个艺术家。行 if(self.artistView == nil)
用于测试 ArtistViewController
是否已初始化;如果尚未初始化,则会创建一个新对象。最后,将 ArtistViewController
推送到 navigationController
堆栈,设置标题属性和 UITextView
的值,然后我们移至下一个视图。
点击 **构建并运行**,应用程序将启动。