Execute a method after n seconds
The whole family performSelector is really interesting and can be useful in a multitude of cases. Its simplest and most common application is as follows:
1 2 3 4 5
| @selector ( myMethod ) withObject : nil afterDelay : 3 ] ; [Self performSelector: @ selector (myMethod) withObject: nil afterDelay: 3]; / / void ) myMethod { - (Void) {myMethod "Hello World!" ) ; NSLog (@ "Hello World!"); } |
However, consider that the "timer" is not accurate. This procedure, therefore, should be used when there is required a "significant" temporal precision.
Retrieve the version of the
1 2
| version = [ [ [ NSBundle mainBundle ] infoDictionary ] objectForKey : @ "CFBundleVersion" ] ; NSString * version = [[[ NSBundle mainBundle] infoDictionary] objectForKey: @ "CFBundleVersion"]; "versione = %@" , version ) ; NSLog (@ "Version =% @", version); |
YES, true or TRUE?
e TRUE sono in pratica la stessa edentica cosa: Going to comb through the Kernel Apple iPhone you can realize that YES , true , and TRUE are basically the same thing Edentia:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| / / Definition of YES # Define YES (BOOL) 1 # Define NO (BOOL) 0
/ / Definition of true # Define true 1 # Define false 0
/ / Definition of TRUE # If! Defined (TRUE) # Define TRUE 1 # Endif
# If! Defined (FALSE) # Define FALSE 0 # Endif |
At least for now ...
Vibration
1 2 3
| # Import <AudioToolbox/AudioToolbox.h> / / ; AudioServicesPlaySystemSound (kSystemSoundID_Vibrate); |
CGImageRef pointer from a UIImage
1 2 3 4
| [ UIImage imageNamed : @ "LittleHeart.png" ] ; UIImage * heart = [UIImage imageNamed: @ "LittleHeart.png"]; heart CGImage ] ; CGImageRef image = [CGImage heart]; / / The image can now be "rasterized" on a CGContextRef CGRect ) { 0 , 0 , 100 , 100 } , image ) ; CGContextDrawImage (c, (CGRect) {0, 0, 100, 100}, image); |
Animations
1 2 3 4 5
| nil context : NULL ] ; [UIView beginAnimations: nil context: NULL]; 1.5 ] ; [UIView setAnimationDuration: 1.5]; UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; / / ... ; [UIView commitAnimations]; |
NSLog
1 2 3
| "NSString object %@ " , myString ) ; NSLog (@ "NSString object% @", myString); "Float: %f " , myFloat ) ; NSLog (@ "float:% f", myFloat); "Integer: %i " , myInt ) ; NSLog (@ "Integer:% i", myInt); |
RGB converter UIColor
1
| # Define RGBA (r, g, b, a) [UIColor colorWithRed: r/255.0 green: blue g/255.0: b/255.0 alpha: a] |
Passing parameters to a NSTimer
Taking advantage of the parameter userInfo you can send a pointer to an object to our method invoked by timer.
1 2 3 4 5 6 7 8 9 10 11 12 13
| scheduledTimerWithTimeInterval : 1 target : self selector : @selector ( timerMethod ) userInfo : objectPointer repeats : YES ] ; [ NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @ selector (timerMethod) userInfo: objectPointer repeats: YES]; / / ... void ) timerMethod : ( NSTimer * ) timer { - (Void) timerMethod: ( NSTimer *) timer { / / Retrieve the pointer to my object timer userInfo ] ; objectPointer = [timer userInfo]; / / Or myMethod ] ; [[Timer userInfo] myMethod]; [ [ timer userInfo ] myProperty ] ; int a = [[timer userInfo] myProperty]; / / Which is the same [ objectPointer myProperty ] ; int a = [objectPointer myProperty]; } |
Running time
Here is a simple way to calculate a short time needed to check the speed of execution of the code:
1 2 3 4
| ; CFAbsoluteTime initialTime CFAbsoluteTimeGetCurrent = (); / / ... tails ; CFAbsoluteTime finalTime CFAbsoluteTimeGetCurrent = (); "Tempo trascorso %f" , finalTime - initialTime ) ; NSLog (@ "elapsed time% f", finalTime - initialTime); |
Continued ...
non funzionano quando un UIButton è impostato in modalità UIButtonTypeCustom ! The states UIControlStateSelected or UIControlStateHighlighted not work when a UIButton mode is set UIButtonTypeCustom ! Or rather, do not work (because reserved for other types of button), for example to create a two-state button: toggle note. If we have created two images (and stato1.png stato2.png) for our button, we can proceed as follows:
1 2 3
| / / The header files that we create a global variable for usaremo / / Check the toggle were ToggleFlag BOOL; |
Now we create our button:
1 2 3 4 5 6 7 8 9
| / / We create a button and we place it initially in the state "stato1.png" / / Edit initWithFrame: (CGRect) {100,100,50,50} with the location and / / Size of your image has ; toggleFlag = YES; [ [ UIButton buttonWithType : UIButtonTypeCustom ] initWithFrame : ( CGRect ) { 100 , 100 , 50 , 50 } ] ; UIButton ToggleButton * = [[UIButton buttonWithType: UIButtonTypeCustom] initWithFrame: (CGRect) {100, 100, 50, 50}]; @ "" forState : UIControlStateNormal ] ; [ToggleButton setTitle: @ "" Forst: UIControlStateNormal]; [ UIImage imageNamed : @ "stato1.png" ] forState : UIControlStateNormal ] ; [ToggleButton setBackgroundImage: [UIImage imageNamed: @ "stato1.png"] Forst: UIControlStateNormal]; self action : @selector ( onToggle : ) forControlEvents : UIControlEventTouchUpInside ] ; [ToggleButton addTarget: self action: @ selector (onToggle:) forControlEvents: UIControlEventTouchUpInside]; toggleButton ] ; [Self.view addSubview: ToggleButton]; |
When you click on the button will send a message to be managed onToggle :
1 2 3 4 5 6 7
| void ) onToggle : ( id ) sender { - (Void) onToggle: (id) sender { / / Retrieve pointer to UIButton ( UIButton * ) sender; UIButton ButtonClicked * = (UIButton *) sender; / / Execute Toogle toggleFlag =! toggleFlag; [ UIImage imageNamed : ( toggleFlag ) ? @ "stato1.png" : @ "stato2.png" ] forState : UIControlStateNormal ] ; [ButtonClicked setBackgroundImage: [UIImage imageNamed: (toggleFlag)? @ "Stato1.png" @ "stato2.png"] Forst: UIControlStateNormal]; } |
Continued ...
An alternative very simple to run a stream a mp3 file on the Apple iPhone could be:
Continued ...
Today, November 25, 2009, was officially opened Mad Ideas , the first factory of ideas generated by users (User Generated Ideas UGI).
Continued ...
Both the Mac OS X and iPhone OS, we can access easily and directly to the directory - most important - the system which are:
1 2 3
| NSHomeDirectory Returns the path to the current user's home directory. Returns the path to NSHomeDirectoryForUser Given a user's home directory. NSTemporaryDirectory Returns the path of the temporary directory for the current user. |
Continued ...
1. Strings on multiple lines
In Xcode, you can "break" a string across multiple lines by inserting at the end with a backslash "\". This feature can be useful when, for example, we want to insert the HTML text in a control UIWebView :
Continued ...
o CGPoint , ad esempio. The syntax NSLog(@"%@", ... ); works and is used to obtain information about objects, but does not work on C data types such as struct CGRect or CGPoint , for example. o NSStringFromCGPoint : To take advantage of NSLog(@"%@", ... ); even on C-style structs can rely on conversion functions like NSStringFromCGRect() or NSStringFromCGPoint :
1 2 3 4 5
| CGRect ) { 10 , 20 , 30 , 40 } ; CGRect mioRect = (CGRect) {10, 20, 30, 40}; CGPoint ) { 32 , 64 } ; CGPoint mioPoint = (CGPoint) {32, 64}; / / "Info rettangolo: %@" , NSStringFromCGRect ( mioRect ) ) ; NSLog (@ "Info rectangle:% @", NSStringFromCGRect (mioRect)); "Info point: %@" , NSStringFromCGPoint ( mioPoint ) ) ; NSLog (@ "Info point:% @", NSStringFromCGPoint (mioPoint)); |
Specifically, it is possible to improve this procedure writes of small useful macros like:
1
| # Define NSLogRect (rect) NSLog (@ "% s (% 0.0f,% 0.0f)% 0.0f% 0.0fx", # rect, rect.origin.x, rect.origin.y, rect.size.width , rect.size.height) |
Or:
1 2 3 4
| # Define NSLogCGPoint (point) NSLog (@ "% s (% 0.0f,% 0.0f)", # point.x point, Point.y)
CGPoint ) { 32 , 64 } ; CGPoint mioPoint = (CGPoint) {32, 64}; ; NSLogCGPoint (mioPoint); |
That will give as output:
1
| 32 , 64 ) mioPoint: (32, 64) |
Continued ...
Application Icon
57 × 57 pixel icon that will represent our application is "impaired" by the Apple iPhone automatically: you add a rounded edge, a bright and 3D effects. This setting can be changed by selecting the file [nome applicazione]-Info.plist and adding the property "Icon includes gloss and bevel effects Already":
Continued ...
If you need to generate random numbers in an application must set aside Apple iPhone Objective-C, because it does not propose any class order. The solution is proposed that the C: rand(), srand(), random(), srandom() e arc4random() .
Continued ...
Latest Comments
Mark : @ Mark: Thanks for the answer. For example, if you have a code to save / use the folder ...
Mark : Thanks for the answer. For example, if you have a code to save / use the folder ...
Giovambattista Fazioli : @ Mark: in principle, absolutely, a lot depends on how and what to download. La. ..
Mark : Hello, good guide! I wanted to ask, you can use the same code to run a sql database? Instead of ...
Joseph : Hi I wanted to ask you how you can do in the post I uploaded a gallery of 50 images, for example, ...