开篇小技巧
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"主屏幕";
if ( !items_ ) {
//将类的名字放进数组
items_ = [[NSArray alloc] initWithObjects:
@"UIKitPrjTextView",
@"UIKitPrjEditableTextView",
@"UIKitPrjWorkingWithTheSelection",
nil ];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:NO];
[self.navigationController setToolbarHidden:NO animated:NO];
// 还原颜色
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
self.navigationController.navigationBar.translucent = NO;
self.navigationController.toolbar.barStyle = UIBarStyleDefault;
self.navigationController.toolbar.translucent = NO;
[UIView setAnimationsEnabled:YES];
}
#pragma mark UITableView methods
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return [items_ count];
}
- (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];
}
NSString* title = [items_ objectAtIndex:indexPath.row];
cell.textLabel.text = [title stringByReplacingOccurrencesOfString:@"UIKitPrj" withString:@""];
return cell;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
//从数组中将类的名取出, 用NSClassFromString( className )真正转化成实体类
NSString* className = [items_ objectAtIndex:indexPath.row];
Class class = NSClassFromString( className );
UIViewController* viewController = [[[class alloc] init] autorelease];
if ( !viewController ) {
NSLog( @"%@ 没有数据.", className );
return;
}
[self.navigationController pushViewController:viewController animated:YES];
}
UITextView* textView = [[[UITextView alloc] init] autorelease];
textView.frame = self.view.bounds;
textView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//textView.editable = NO; //< 不可编辑
textView.backgroundColor = [UIColor blackColor]; //< 背景为黑色
textView.textColor = [UIColor whiteColor]; //< 字符为白色
textView.font = [UIFont systemFontOfSize:32]; //< 字体的设置
textView.text = @"学习UITextView!\n"
"第2行\n"
"第3行\n"
"4行\n"
"第5行\n"
"第6行\n"
"第7行\n"
"第8行\n"
"第9行\n"
"第10行\n"
"第11行\n"
"第12行\n";
[self.view addSubview:textView];
- (void)dealloc {
[textView_ release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
textView_ = [[UITextView alloc] init];
textView_.frame = self.view.bounds;
textView_.autoresizingMask = UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
textView_.delegate = self;
textView_.text = @"亲们,可以编辑这一段文本。";
[self.view addSubview:textView_];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self textViewDidEndEditing:textView_]; //< 画面显示时设置为非编辑模式
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[textView_ resignFirstResponder]; //< 画面跳转时设置为非编辑模式
}
- (void)textViewDidBeginEditing:(UITextView*)textView {
static const CGFloat kKeyboardHeight = 216.0;
// 按钮设置为[完成]
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneDidPush)] autorelease];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
// 缩小UITextView以免被键盘挡住
CGRect textViewFrame = textView.frame;
textViewFrame.size.height = self.view.bounds.size.height - kKeyboardHeight;
textView.frame = textViewFrame;
// 工具条位置上移
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y =
self.view.window.bounds.size.height - toolbarFrame.size.height - kKeyboardHeight;
self.navigationController.toolbar.frame = toolbarFrame;
[UIView commitAnimations];
}
- (void)textViewDidEndEditing:(UITextView*)textView {
// 按钮设置为[编辑]
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(editDidPush)] autorelease];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
// 恢复UITextView的尺寸
textView.frame = self.view.bounds;
// 恢复工具条的位置
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y =
self.view.window.bounds.size.height - toolbarFrame.size.height;
self.navigationController.toolbar.frame = toolbarFrame;
[UIView commitAnimations];
}
- (void)editDidPush {
[textView_ becomeFirstResponder];
}
- (void)doneDidPush {
[textView_ resignFirstResponder];
}
#import "UIKitPrjWorkingWithTheSelection.h"
static const CGFloat kKeyboardHeight = 216.0;
@implementation UIKitPrjWorkingWithTheSelection
- (void)dealloc {
[textView_ release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// UITextView的追加
textView_ = [[UITextView alloc] init];
textView_.frame = self.view.bounds;
textView_.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
textView_.text = @"此文本可编辑。";
[self.view addSubview:textView_];
// 在工具条中追加按钮
UIBarButtonItem* hasTextButton =
[[[UIBarButtonItem alloc] initWithTitle:@"hasText"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(hasTextDidPush)] autorelease];
UIBarButtonItem* selectionButton =
[[[UIBarButtonItem alloc] initWithTitle:@"selection"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(selectionDidPush)] autorelease];
UIBarButtonItem* alignmentButton =
[[[UIBarButtonItem alloc] initWithTitle:@"alignment"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(alignmentDidPush)] autorelease];
UIBarButtonItem* scrollButton =
[[[UIBarButtonItem alloc] initWithTitle:@"top"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(scrollDidPush)] autorelease];
NSArray* buttons = [NSArray arrayWithObjects:hasTextButton, selectionButton, alignmentButton, scrollButton, nil];
[self setToolbarItems:buttons animated:YES];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// 调整工具条位置
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
textView_.frame =
CGRectMake( 0, 0, self.view.bounds.size.width, self.view.bounds.size.height - kKeyboardHeight );
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y =
self.view.window.bounds.size.height - toolbarFrame.size.height - kKeyboardHeight;
self.navigationController.toolbar.frame = toolbarFrame;
[UIView commitAnimations];
[textView_ becomeFirstResponder]; //< 画面显示时显示键盘
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 恢复工具条
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
textView_.frame = self.view.bounds;
CGRect toolbarFrame = self.navigationController.toolbar.frame;
toolbarFrame.origin.y = self.view.window.bounds.size.height - toolbarFrame.size.height;
self.navigationController.toolbar.frame = toolbarFrame;
[UIView commitAnimations];
[textView_ resignFirstResponder]; //< 画面隐藏时隐藏键盘
}
- (void)hasTextDidPush {
UIAlertView* alert = [[[UIAlertView alloc] init] autorelease];
if ( textView_.hasText ) {
alert.message = @"textView_.hasText = YES";
} else {
alert.message = @"textView_.hasText = NO";
}
[alert addButtonWithTitle:@"OK"];
[alert show];
}
- (void)selectionDidPush {
UIAlertView* alert = [[[UIAlertView alloc] init] autorelease];
alert.message = [NSString stringWithFormat:@"location = %d, length = %d",
textView_.selectedRange.location, textView_.selectedRange.length];
[alert addButtonWithTitle:@"OK"];
[alert show];
}
- (void)alignmentDidPush {
textView_.editable = NO;
if ( UITextAlignmentRight < ++textView_.textAlignment ) {
textView_.textAlignment = UITextAlignmentLeft;
}
textView_.editable = YES;
}
- (void)scrollDidPush {
// NSRange scrollRange = NSMakeRange( 0, 1 );
[textView_ scrollRangeToVisible:NSMakeRange( 0, 1 )];
}