详解iPhone SDK 开发之 UIKit 使用是本文要介绍的内容,关于UIKit,你可以使用UIKit框架来建立和管理iPhone应用程序的用户界面。这个Objective-C框架特别为Multi-Touch界面提供了一个应用程序对象、事件处理、绘图模型、窗口、视图和控件。
UIKit使用2
2. View Controllers
可以使用UIViewController类来创建和显示多个view, 就像前一个例子里MainView来控制TextView一样.
UIViewController还提供旋转(例如横握或竖握你的iphone)你的view,或低内存报警等功能.
2.1 创建一个view controller
(1)从UIViewController继承一个自己的view controller
#import
#import
@interface MainViewController : UIViewController {
UITextView *textView;
}
//默认的初始化函数用init,而不是initWithFrame
- (id)init;
- (void)dealloc;
//系统会调用loadView来安排你自己的子view
- (void)loadView;
@end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
(2) UIViewController会自动创建一个UIView对象 self.view, 你可以把自己的view添加到这个self.view里去,例如下面的例子:垂直显示两个text view.
(void)loadView {
CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];
textView1 = [ [ UITextView alloc ] initWithFrame:
CGRectMake(0, 0, bounds.size.width, bounds.size.height / 2)
];
textView2 = [ [ UITextView alloc ] initWithFrame:
CGRectMake(0, bounds.size.height / 2,
bounds.size.width,
bounds.size.height / 2)
];
textView1.text = @"Hello, World!";
textView2.text = @"Hello again!";
[ self.view addSubview: textView1 ];
[ self.view addSubview: textView2 ];
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
(3)当然你也可以把self.view整个替换成自己的view
(void)loadView {
[ super loadView ];
CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];
textView = [ [ UITextView alloc ] initWithFrame: bounds ];
textView.text = @"Hello, World! ";
self.view = textView;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
(4)一般loadView只会被调用一次, 但是当内存不够用的时候,
UIViewController会调用didReceiveMemoryWarning方法, 你可以在这个方法里释放自己的资源, 然后loadView会被重新自动调用.
2.2 使用interface builder
你可以用UIViewController类的initWithNibName方法加载interface builder创建的.xib资源文件.
MainViewController *myViewController = [
[ MainViewController alloc ]
initWithNibName: @"MainViewController"
bundle: nil
];
- 1.
- 2.
- 3.
- 4.
- 5.
2.3 方向改变
(1)系统通过shouldAutorotateToInterfaceOrientation来检查是否可以旋转到interfaceOrientation所指示的方向.
(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
return (YES);
}
UIDeviceOrientationUnknown //Catchall for errors or hardware failures
UIDeviceOrientationPortrait //Oriented upright vertically in portrait mode
UIDeviceOrientationPortraitUpsideDown //Oriented upside-down vertically in portrait mode
UIDeviceOrientationLandscapeLeft //Device is rotated counter-clockwise in landscape mode
UIDeviceOrientationLandscapeRight //Device is rotated clockwise in landscape mode
UIDeviceOrientationFaceUp //Device is laying flat, face up, such as on a table
UIDeviceOrientationFaceDown //Device is laying flat, face down, such as on a table
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
(2)当方向改变时,系统会调用didRotateFromInterfaceOrientation
(void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation
{
}
- 1.
- 2.
- 3.
- 4.
2.4 清除view controller
(void)dealloc {
[ textView release ];
[ super dealloc ];
}
- 1.
- 2.
- 3.
- 4.
2.5 Controller demo
Example 3-7. ControllerDemo application delegate prototypes (ControllerDemoAppDelegate.h)
#import
@class ControllerDemoViewController;
@interface ControllerDemoAppDelegate : NSObject {
UIWindow *window;
ControllerDemoViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet ControllerDemoViewController *viewController;
@end
Example 3-8. ControllerDemo application delegate (ControllerDemoAppDelegate.m)
#import "ControllerDemoAppDelegate.h"
#import "ControllerDemoViewController.h"
@implementation ControllerDemoAppDelegate
@synthesize window;
@synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
CGRect screenBounds = [ [ UIScreen mainScreen ] bounds ];
self.window = [ [ [ UIWindow alloc ] initWithFrame: screenBounds ]
autorelease
];
viewController = [ [ ControllerDemoViewController alloc ] init ];
[ window addSubview:viewController.view ];
[ window makeKeyAndVisible ];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
@end
Example 3-9. ControllerDemo view controller prototype (ControllerDemoViewController.h)
#import
#import
@interface ControllerDemoViewController : UIViewController {
NSString *helloWorld, *woahDizzy;
UITextView *textView;
}
@end
Example 3-10. ControllerDemo view controller (ControllerDemoViewController.m)
#import "ControllerDemoViewController.h"
@implementation ControllerDemoViewController
- (id)init {
self = [ super init ];
if (self != nil) {
helloWorld = [ [ NSString alloc ] initWithString: @"Hello, World!" ];
woahDizzy = [ [ NSString alloc ] initWithString: @"Woah, I'm Dizzy!" ];
}
return self;
}
- (void)loadView {
[ super loadView ];
textView = [ [ UITextView alloc ] initWithFrame:
[ [ UIScreen mainScreen ] applicationFrame ]
];
textView.text = helloWorld;
self.view = textView;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)didRotateFromInterfaceOrientation:
(UIInterfaceOrientation)fromInterfaceOrientation
{
textView.text = woahDizzy;
}
- (void)viewDidLoad {
[ super viewDidLoad ];
}
- (void)didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
}
- (void)dealloc {
[ helloWorld release ];
[ woahDizzy release ];
[ textView release ];
[ super dealloc ];
}
@end
Example 3-11. ControllerDemo main (main.m)
#import
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"ControllerDemoAppDelegate");
[pool release];
return retVal;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
小结:详解iPhone SDK 开发之 UIKit 使用的内容介绍完了希望本文对你有所帮助。