Running a Method After A Delay on iPhone or Mac
2011-11-26 10:44
225 查看
In nearly every programming language there is a way to delay a method call. Objective-C is no different. They have built in a few simple methods to achieve this. We are going take a look at one of these methods today, all which reside in NSObject. This means
any class you create will have the ability to call these methods. The method we are going to take a gander at is
got the love the Objective-C method names.
This method is pretty self explanatory thanks to the super long name. Basically, we are going to run a method and pass in the object as the parameter for the method after a certain amount of time (in seconds). Okay, imagine we have built an application which
has a label that we want to change the text of after 5 seconds, why? I don't know, if the label keeps the same text for more than 5 seconds it will self destruct, anyway I digress. So, we create a method to change the text to something like the following:
-(void)changeText:(NSString*)text
{
myLabel.text = text;
}
The new method is about as simple as it gets, it changes the label's text to the passed in value. Now, in order to call this on a delay we need to use our aforementioned method,
In order to call our change text method with this it would look something like below.
[self performSelector:@selector(changeText:)
withObject:@"Hello" afterDelay:5];
This pretty much covers it. One note is that say you have a method that doesn't take a parameter like the one below.
-(void)changeText
{
myLabel.text = @"Hello";
}
Then if you want to call the method you pass in
like the following:
[self performSelector:@selector(changeText)
withObject:nil afterDelay:5];
另外一个延时方法可以用sleep(4)函数
any class you create will have the ability to call these methods. The method we are going to take a gander at is
-(void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay,
got the love the Objective-C method names.
This method is pretty self explanatory thanks to the super long name. Basically, we are going to run a method and pass in the object as the parameter for the method after a certain amount of time (in seconds). Okay, imagine we have built an application which
has a label that we want to change the text of after 5 seconds, why? I don't know, if the label keeps the same text for more than 5 seconds it will self destruct, anyway I digress. So, we create a method to change the text to something like the following:
-(void)changeText:(NSString*)text
{
myLabel.text = text;
}
The new method is about as simple as it gets, it changes the label's text to the passed in value. Now, in order to call this on a delay we need to use our aforementioned method,
-(void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay.
In order to call our change text method with this it would look something like below.
[self performSelector:@selector(changeText:)
withObject:@"Hello" afterDelay:5];
This pretty much covers it. One note is that say you have a method that doesn't take a parameter like the one below.
-(void)changeText
{
myLabel.text = @"Hello";
}
Then if you want to call the method you pass in
nilfor the
withObjectargument,
like the following:
[self performSelector:@selector(changeText)
withObject:nil afterDelay:5];
另外一个延时方法可以用sleep(4)函数
相关文章推荐
- RemoteBox : Screenshots Screenshots Various screenshots of RemoteBox under different operating systems including Linux, MacOS X, Solaris, NetBSD, FreeBSD and OpenBSD. Do you have a screenshot of RemoteBox running on a distro or operating system
- How to show or hide filename extensions on Mac
- MOSS爬网问题Error from SharePoint site: Data is Null. This method or property cannot be called on Null values
- Fragment Or DialogFragment Can not perform this action after onSaveInstanceState
- After upgrading the ebPart Assembly "Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed .."
- host application or driver claiming the device on macosx
- Mac Error Create Android Project - “Errors running builder 'Android Resource Manager' on project”
- Data is Null. This method or property cannot be called on Null values.错误分析
- IOS 被拒 关于 iPhone running iOS 10.3.1 on Wi-Fi connected to an IPv6 network.
- Getting started with aspnet5 and visualstudio code on a mac or linux
- REMOVE ONCLICK DELAY ON WEBKIT FOR IPHONE
- #include "CLucene.h" After a rather long search for the cause Strigi crash on PPC (e.g. Mac)use the flag -ansi when compiling c++ code
- mac下* daemon not running.starting it now on port 5037 * 问题解决
- COCOSDX2.2如何在Mac OS X 中运行Lua (Running Lua on Mac OS X)
- How to use Display Zoom on your iPhone 6 or iPhone 6 Plus
- Sleeping with the Enemy: Running Windows® on the Mac®, Digital Shortcut
- 如何在Mac OS X 中运行Lua (Running Lua on Mac OS X)
- xcode真机调试提示Finished running *** on iPhone
- Permissions problems after running Qt Creator with priviliges on Linux(ubuntu)
- Errors running builder "Integrated External Tool Builder" on project or cannot set breakpoint