iPad开发新手常见问题
kagula
2011-6-28
正文
Q1: 代码的组织
A: 主要的组织形式:由xib文件及其对应的h和m文件组成一个窗口(结点),以某个结点作为根结点,开支散叶,形成倒挂的树。用户通过窗口和程序进行交互。窗口内至少有一个底视图(View),其它View可以拖放到这个底视图中形成复杂的可交互的窗口,用户通过View和具体代码交互。
Q2: 窗口(结点)如何加载另一个窗口(结点)
A: 联系方式有两种:
第一种静态连接:假设有两个xib结点,一个名为A,另一个名为B,A要调用B。首先A的xib文件里面添加View Controller,设置View Controller的属性为B的xib文件和B的Class。第二,在A的源码中添加指向B的IBOutlet。第三,通过拖放把两者之间联系起来。这个方法最大的缺点是,你的APP启动时不管你现在有没有用到B,都会被装载,占用内存,因此只适合窗口数量比较少的情况。
最后一种方法为动态连接:只在需要的时候加载,但是最大的缺点是你要自己release。
弹出方式主要有三种:第一种,叠加视图,使用addSubView方法。
第二种,模式对话框方式弹出视图,由弹出者负责设置弹出窗口的大小,示例代码如下,。
-(IBAction)popupView:(id)sender
{
myModalViewController.modalPresentationStyle = UIModalPresentationFormSheet;
myModalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:myModalViewController animated:YES];
myModalViewController.view.superview.frame = CGRectMake(0,0,320,200);
myModalViewController.view.superview.center = self.view.center;
}
第三种,无模式对话框方式弹出窗口,示例如下,使用了动态加载方式加载另一个窗口:
//@interface MyPopOverView : UIViewController
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if(![popoverController isPopoverVisible]){
// Popover is not visible
myPopOver = [[MyPopOverView alloc] initWithNibName:@"MyPopOverView" bundle:nil];
popoverController = [[UIPopoverController alloc] initWithContentViewController:myPopOver];
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
NSLog(@"location=%f,%f",location.x,location.y);
popoverController.popoverContentSize=CGSizeMake(320, 200);
[popoverController presentPopoverFromRect:CGRectMake(location.x, location.y, 320, 200)
inView:self.view permittedArrowDirections:NO animated:YES];
}else{
[popoverController dismissPopoverAnimated:YES];
}
[super touchesBegan:touches withEvent:event];
}
Q3:自定义列表框要注意的地方
A:自定义列表框主要有两种方法,建议继承UITableViewCell的方式新建类,参考文档[1]这里要注意的是Xib中的View中的元素要拉到View而不是File’Owner。
Q4:为选词浮标添加菜单项
A:仅给出示例代码
//第一步
- (void)viewDidLoad {
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"新的菜单项" action:@selector(infoButtonPressed:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObject:menuItem]];
[menuItem release];
[super viewDidLoad];
}
//第二步
//激发infoButtonPressed后,从txtView控件中选择的单词
-(void)infoButtonPressed:(id)sender
{
NSString *selectedText = [txtView.text substringWithRange:txtView.selectedRange];
NSLog(@"%@",selectedText);
g_params = selectedText;
myModalViewController.oneLabel.text = selectedText;
[self popupView:sender];
}
Q5:UIScrollView的使用要注意的地方
A:[1]添加手势后,UIScrollView的canCancelContentTouches 属性要设为 NO,如果你想取到UIScrollView里的UIButton控件的事件的话。[2]横屏竖屏切换后,最好把UIScrollView的zoomScale属性设为1.0。 其它事项参考资料[2]
Q6:横屏竖屏幕切换
A:使用下面的示例来说明使用
//第一步:响应事件
-(void) willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self screentz];
}
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self screentz];
}
//第二步:实现screentz
-(void)screentz
{
UIInterfaceOrientation destorien=self.interfaceOrientation;
if (destorien==UIInterfaceOrientationPortrait||destorien==UIInterfaceOrientationPortraitUpsideDown) {
tableView.frame=CGRectMake(0,64, 320, 640);
}
else {
tableView.frame=CGRectMake(0, 64, 320, 640);
}
}
Q7:隔断时间自动隐藏控件
A: 下面是示例代码
- (void)runToolbarHidingTimer;
{
toolbarHidingTimer = [NSTimer scheduledTimerWithTimeInterval: 10.0f
target: self
selector: @selector(hideToolbarAnimated)
userInfo: nil
repeats: NO];
}
- (void)showToolbar
{
[toolbarHidingTimer invalidate];
toolbarHidingTimer = nil;
self.toolbar.hidden=NO;
[self.toolbar setAlpha:1.0];
}
- (void)hideToolbarAnimated
{
toolbarHidingTimer = nil;
CGContextRef context = UIGraphicsGetCurrentContext();
// Initialize Animation
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[self.toolbar setAlpha:0.0];
// Animation Settings
[UIView commitAnimations];
}
参考文档:
[1]《Custom UITableViewCells with Interface Builder》
http://www.e-string.com/content/custom-uitableviewcells-interface-builder
[2]《Using UIScrollView and UIGestureRecoginzer to create a custom interface - Part II》
http://www.waynewbishop.com/code/uiscrollview/part2