Delay Actions
In this tutorial i will be showing you how to delay actions
Features:
Delaying action in you apps is very useful enabling you to hide content in your apps before revealing it to your user or create splash screens switching views to you main view after a certain period of time
Play the video to get a step by step walkthrough and all code can be copy and pasted
ViewController.h |
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { __weak IBOutlet UILabel *label1; __weak IBOutlet UILabel *label2; } - (IBAction)pushme:(id)sender; - (void)delay1; - (void)delay2; @end
Import a new view and name is secondview
ViewController.m
#import "ViewController.h" #import "secondview.h" - (void)viewDidUnload { label1 = nil; label2 = nil; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (IBAction)pushme:(id)sender { label1.text = @"In 5 seconds the second label will be displayed"; [self performSelector:@selector(delay1) withObject:nil afterDelay:5.0]; } - (void)delay1 { label2.text = @"In 5 seconds the view will switch"; [self performSelector:@selector(delay2) withObject:nil afterDelay:5.0]; } - (void)delay2 { secondview *second = [[secondview alloc] initWithNibName:nil bundle:nil]; [self presentModalViewController:second animated:YES]; }
secondview.h
#import <UIKit/UIKit.h> @interface secondview : UIViewController { } - (IBAction)dismiss:(id)sender; @end
secondview.w
- (IBAction)dismiss:(id)sender { [self dismissModalViewControllerAnimated:YES]; }
视频:http://www.youtube.com/watch?feature=player_embedded&v=lOp9-fiH3BI
分解:
1. @selector 是什么?
1、一种类型 SEL
2、代表你要发送的消息(方法), 跟字符串有点像, 也可以互转.: NSSelectorFromString() / NSSelectorFromString()
3、可以理解为类似函数指针的东西--是能让Objective-C动态调用方法的玩意.--是 object-c 的动态后绑定技术 可以通过字符串 访问的函数指针
4、其实就是消息响应函数---选一个消息响应的函数地址给你的action
5、@selector(function_name) 即取得一个function的id
参考: Objective-C 2.0 with Cocoa Foundation--- 5,Class类型,选择器Selector以及函数指针
2. 关于performSelector: withObject:afterDelay:方法
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
aSelector是你想调用的方法,可带一个参数或者是无参数,如果是一个参数的话这个参数必须是对象类型,如:NSString,NSArray之类的,不能是NSInteger这样的,
anArgument是你要传递
的参数,函数无参数时为nil,
delay:过多久调用这个方法.
3. initWithNibName 这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的.
4. presentModalViewController和dismissModalViewControllerAnimated
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated; // Display another view controller as a modal child. Uses a vertical sheet transition if animated. - (void)dismissModalViewControllerAnimated:(BOOL)animated; // Dismiss the current modal child. Uses a vertical sheet transition if animated.
在iOS当中,若是要切换页面,除了使用Navigation外,也可以在UIViewController使用presentModalViewController方法,将其他的View推进来。
方法:
secondview *second= [[secondview alloc] initWithNibName:@"secondview" bundle:nil]; self presentModalViewController:second animated:YES];
用法很简单,只要将要推进来的View建立起来,再利用presentModalViewController方法即可。
若要将利用上述方法推进来的View退掉,就可以使用dismissModalViewControllerAnimated方法。
[self dismissModalViewControllerAnimated:YES];
但若是你的程式的架构比较复杂,比如说:在window上有超过一层的View,可能会在推进View的时候造成方向上的错乱,则可以利用设定modalPresentationStyle为UIModalPresentationCurrentContext来解决。
secondview.modalPresentationStyle = UIModalPresentationCurrentContext;
除了UIModalPresentationCurrentContext以外,还有三种PresentationStyle:
1. UIModalPresentationFormSheet:view以视窗的方式出现,很像网页上js遮照的效果。
2. UIModalPresentationFullScreen:整页覆盖。
3. UIModalPresentationPageSheet:直的时候是FullScreen效果,横的时候是FormSheet效果。
除了控制出现的区块,也可以控制出现的动画,利用设定modalTransitionStyle完成。
secondview.modalTransitionStyle = UIModalTransitionStyleCoverVertical;