简介
我没有得到长期发展iPhone应用程序,但我希望,这个简单的教程会给你一个清晰的思路,如何去:创建一个基于导航的应用程序创建新的类对象一个对象数组填充一个UITableView之间转换视图步骤1:创建一个新项目
启动的X代码,并选择文件GT;新建项目GT GT基于导航的应用;选择。
{S0}
该项目名称为"MusicApp"。第2步:创建我们的对象
下一步是创建一个"艺术家"类继承自NSObject。我们将使用它来创建应用程序的第一个屏幕上,可以由用户选择的对象。
单击"文件">新建文件GT;的Objective - C类,并选择NSObject子类。
类的名称为"艺术家"和确保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说明:(NSString的*)DESC;是声明一个函数是用来初始化我们的艺术家对象。所有NSObjects有一个init方法,但我们将创造我们自己,为了传递额外的参数来初始化对象。
将以下代码添加到Artist.m:{C}
在上面,我们已经实施了initWithName方法,该方法有两个参数,将它们分配给我们的局部变量,就像任何其他构造。行回归自我;需要返回一个新的对象实例。第三步:视图之间的转换
我们要做的第一件事是设置在第一个屏幕上的标题。这是必要的,以创建一个后退按钮。
将以下代码添加到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,我们将创建后(当用户选择一个艺术家,我们显示这种看法)。该生产线@合成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;
声明数组之后,我们现在需要创造我们的艺术家对象。
将以下代码添加到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];
}
首先要注意的是,我们已经添加@合成艺术家在上面。这是使其他对象可以访问我们已宣布的阵列。
在applicationDidFinishLaunching方法,我们已经建立了三个艺术家的对象,使用前面我们写的构造。行self.artists = [[NSMutableArray ALLOC] initWithObjects:artist1,artist2,artist3,零]创建一个新的阵列,其中包含我们的艺术家对象,并把它分配到我们已宣布的阵列。请注意,零是最后一个参数是需要结束阵列。
当用户选择一个艺术家的第一个屏幕上,我们要显示在第二个他们的传记。称为"ArtistViewController" - 文件>新建文件,创建一个新的ViewController。
{S2}
确保用户界面随着厦门国际银行"被选中。 XIB文件是呈现给用户的接口。双击XIB文件和Interface Builder的负载。拖到视图的UITextView,这将被用来显示艺术家的传记。保存并关闭界面生成器。
{S3}
ArtistViewController.h,添加以下代码:#import <uikit>
@interface ArtistViewController : UIViewController {
IBOutlet UITextView *artistBio;
}
@property(nonatomic,retain) IBOutlet UITextView *artistBio;
@end
在这里,我们创建一个界面生成器插座,以便我们可以关联UITextView的财产,并在屏幕上显示。添加行@合成artistBio; @执行下面的ArtistViewController.m,创建的artistBio属性的getter和setter方法。
现在,我们需要连接电源插座的UITextView的。为了做到这一点,双击ArtistViewController.xib文件再次打开Interface Builder的。点击文件的所有者和工具GT;连接督察。只需将插座上的UITextView的,然后保存并关闭界面生成器。
{S4}的
为了过渡到这一观点,我们需要声明一个变量,在最初的ViewController。
将以下代码添加到RootViewController.h:#import "ArtistViewController.h"
@interface RootViewController : UITableViewController {
ArtistViewController *artistView;
}
@property(nonatomic,retain)ArtistViewController *artistView;
@end
通过添加行的#import"ArtistViewController.hquot;,我们可以创建新的ArtistViewController实例。步骤4:显示和选择对象
要告诉第一的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)艺术家数组返回一个艺术家。然后设置单元格的文本属性是艺术家的名字。
最后一个步骤是确定用户选择了其中的艺术家。在方法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)获得艺术家阵列。该生产线(self.artistView无)测试来看看,如果ArtistViewController已经初始化,如果没有,将创建一个新的对象。最后,ArtistViewController是推到navigationController堆栈,设置标题属性和UITextView的价值,我们移动到下一个视图。
单击"生成和进入,应用程序将启动。
{五}
参考文献{A}