Archive July, 2009


iPhone: Create a way to respond to events that cross class

Normally an event, which is nothing but a message is fixed (set and implemented) in the same class or context, the function or procedure "caller". o in un UIViewController . For example if we add a button UIButton via code (programmatically), we can find our class in a UIView or a UIViewController . In both cases the task allocation and initialization of the button will be followed by the setting of target that should receive a message when it "clicks" the button, type:

1
2
3
4
5
6
7
8
9
[ UIButton buttonWithType : UIButtonTypeRoundedRect ] ; UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
10 , 180 , 300 , 30 ) ; bottone.frame = CGRectMake (10, 180, 300, 30);
@ "Press me" forState : UIControlStateNormal ] ; [Button setTitle: @ "Press me" forState: UIControlStateNormal];
/ / Decide who should receive the message UIControlEventTouchUpInside
self action : @selector ( onButtonClicked ) forControlEvents : UIControlEventTouchUpInside ] ; [AddTarget button: self action: @ selector (onButtonClicked) forControlEvents: UIControlEventTouchUpInside];
/ / ...
void ) onButtonClicked { - (Void) {onButtonClicked
/ / ...
}

Line 5 of the code shown above decides who (subject) and what (method) "call" when our button is pressed. In the example illustrated above is also known that the setting pressure of the message is sent to the method onButtonClick implemented below, and then forming part of the same context (or class). potremmo inviare il nostro messaggio ad un qualsiasi altro oggetto, posto quindi al difuori del contesto in uso. The first consideration is obvious that we can do, then, is that by altering the parameters self and action we can send our message to any other object, then place also outside of the context in use. : Here's an example: a class UIApplicationDelegate create a UIViewController :

1
2
3
4
5
/ /
/ / MyAppDelegate.m
/ /
SplashScreenController alloc ] ; splashScreenController = [SplashScreenController alloc];
splashScreenController.view ] ; [Window addSubview: splashScreenController.view];

associata al UIViewController stesso: The SplashScreenController exposes a method that allows you to animate the UIView associated with UIViewController same:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/ /
/ / SplashScreenController.m
/ /
void ) animateBackgroundDown { - (Void) {animateBackgroundDown
nil context : nil ] ; [UIView beginAnimations: nil context: nil];
0.75 ] ; [UIView setAnimationDuration: 0.75];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
self ] ; [UIView setAnimationDelegate: self];
@selector ( onAnimationFinished ) ] ; [UIView setAnimationDidStopSelector: @ selector (onAnimationFinished)];
CGRect ) { 0 , 480 , 320 , 480 } ; self.view.frame = (CGRect) {0, 480, 320, 480};
; [UIView commitAnimations];
}
/ / ...
void ) onAnimationFinished { - (Void) {onAnimationFinished
"Animazione terminata" ) ; NSLog (@ "Animation terminated");
}

. The above code shows a method defined inside the class SplashScreenController type UIViewController . ) onAnimationFinished definito più sotto, facente parte sempre della classe SplashScreenController . It does nothing but animate the UIView animating it down, and when the animation is complete, call (send a message to itself - hence self ) onAnimationFinished defined below, is part of the class always SplashScreenController . , non saremo informati della fine dell'animazione: It follows that in our myAppDelegate , when we invoke the method animateBackgroundDown , we will not be informed of the end of the animation:

1
2
3
4
/ /
/ / MyAppDelegate.m
/ /
; [SplashScreenController animateBackgroundDown];

What we want, instead, is to create a new version of animateBackgroundDown as to tell him where to send the message end animation and which method to call. In practice we want to make sure you can write in our class myAppDelegate :

1
2
3
4
5
6
7
8
/ /
/ / MyAppDelegate.m
/ /
self selector : @selector ( onAnimationFinished ) ] ; [SplashScreenController animateBackgroundDown: self selector: @ selector (onAnimationFinished)];
/ / ...
void ) onAnimationFinished { - (Void) {onAnimationFinished
"Animazione terminata" ) ; NSLog (@ "Animation terminated");
}

, bensì in myAppDelegate . This time the method onAnimationFinished is not in the UIViewController , but in myAppDelegate . nel modo seguente: To do this simply change the method animateBackgroundDown in UIViewController as follows:

1
2
3
4
5
6
7
8
9
10
11
12
/ /
/ / SplashScreenController.m
/ /
void ) animateBackgroundDown : ( id ) target selector : ( SEL ) selector { - (Void) animateBackgroundDown: (id) target selector: (SEL) selector {
nil context : nil ] ; [UIView beginAnimations: nil context: nil];
0.75 ] ; [UIView setAnimationDuration: 0.75];
UIViewAnimationCurveEaseInOut ] ; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
target ] ; [UIView setAnimationDelegate: target];
selector ] ; [UIView setAnimationDidStopSelector: selector];
CGRect ) { 0 , 480 , 320 , 480 } ; self.view.frame = (CGRect) {0, 480, 320, 480};
; [UIView commitAnimations];
}

). Now we have a method that accepts the "context" ( target ) and the method to call ( selector ). a qualsiasi altro “oggetto” / classe in grado di riceverlo. Now, when the animation ends, the message AnimationDidStop will be sent to myAppDelegate any other "object" / class that can receive it.

More ...

Objective-C: an alternative to using CGRectMake

CGRectMake() is a function (actually an inline # define) used a lot especially when objects are created by code or graphical user interface. o UIImageView . CGRectMake() restituisce una struct (struttura di tipo) CGRect : Its use is therefore often associated initialization components of UIKit , but also to simple UIView or UIImageView . CGRectMake() returns a struct (structure type) CGRect :

1
2
3
4
5
struct {CGRect
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;

: Which in turn is composed of two different struct CGPoint and CGSize :

1
2
3
4
5
6
7
8
9
10
11
12
13
struct {CGPoint
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;

/ * Sizes. * /

struct {CGSize
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;

. That, in their turn again, contain types CGFloat , or types float . If we analyze the code of CGRectMake () are:

1
2
3
4
5
6
7
8
CG_INLINE CGRect
CGRectMake (CGFloat x, y CGFloat, CGFloat width, height CGFloat)
{
CGRect rect;
y; rect.origin.x = x; rect.origin.y = y;
height; rect.size.width = width; rect.size.height = height;
return rect;
}

It follows, therefore, that this part of the code:

1
2
3
4
[ UIButton buttonWithType : UIButtonTypeRoundedRect ] ; UIButton gbutton * = [UIButton buttonWithType: UIButtonTypeRoundedRect];
12 , 409 , 100 , 40 ) ; gbutton.frame = CGRectMake (12, 409, 100, 40);
@ "Press" forState : UIControlStateNormal ] ; [Gbutton setTitle: @ "Press" forState: UIControlStateNormal];
gbutton ] ; [MainWindow addSubview: gbutton];

It could rightly be written as:

1
2
3
4
[ UIButton buttonWithType : UIButtonTypeRoundedRect ] ; UIButton gbutton * = [UIButton buttonWithType: UIButtonTypeRoundedRect];
CGRect ) { 12 , 409 , 100 , 40 } ; gbutton.frame = (CGRect) {12, 409, 100, 40};
@ "Press" forState : UIControlStateNormal ] ; [Gbutton setTitle: @ "Press" forState: UIControlStateNormal];
gbutton ] ; [MainWindow addSubview: gbutton];

Just to speed things up 'the code is running ... :)

More ...

Xcode shortcut

Xcode is a really good development environment, nice and full of details that make writing efficient code and pleasant. Among these is certainly the convenience of auto-complete when typing, especially when writing applications for Apple iPhone, where the frameworks are many and remember syntax and nomenclature is undertaking a few.

More ...

XCode: Tips on Debugging Preferences

Using the Xcode Preferences you can set the behavior of the atmosphere during the debugging phase of an iPhone application. The default settings, in fact, are very uncomfortable when you try and try again an application, for example, after launching our application, you must manually open the Console window to see the output of various NSLog() . Xcode also lets the previous sessions so we had to clean out the window by hand. Fortunately, you can solve the problem by turning the Preferences:

xcode-preferences

As shown in the figure above, simply select an item from the Start Menu On the debug window to decide which open automatically on startup of our appicazione (I set Console & Debugger but you can choose those that best suits you). On the right, then we find Auto Clear Debug Console, so you always start with the Console clean.

More ...

iPhone: extend a class UIView

creato. I want to show how it is possible, in a simple and rapid, extend a class UIView in such a way that responds to a new initialization method further containing the object code to customize UIView created. As some of you probably know, Objective-C does not support a single manufacturer as in other object oriented languages ​​(such as Adobe ActionScript or PHP5). E 'can, in fact, create an instance of a class manually invoking different manufacturers (virtually infinite). . Normally we are used to create objects UIView with the classic initWithFrame . Suppose, however, we want to create a new class of visual objects, starting from a UIView , with some addition, such a label is incorporated. . Basically what we want is that instantiating our object it creates, as well as the UIView , an object of type UILabel . For example we might want to get to the end:

1
[ [ ViewLabel alloc ] initWithLabelDefine : CGRectMake ( 0 , 0 , 320 , 80 ) label : @ "Ciao" ] ; ViewLabel * vl = [[ViewLabel alloc] initWithLabelDefine: CGRectMake (0, 0, 320, 80) label: @ "Hello"];

: To do this simply create a new class of type UIView and call ViewLabel :

1
2
3
4
5
6
7
8
9
10
11
12
13
/ /
/ / ViewLabel.h
/ /

# Import <UIKit/UIKit.h>

UIView { @ Interface ViewLabel: UIView {
UILabel * internalLabel;
}

id ) initWithLabelDefine : ( CGRect ) frame label : ( NSString * ) labelDefine; - (Id) initWithLabelDefine: (CGRect) frame label: ( NSString *) labelDefine;

@ End
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/ /
/ / ViewLabel.m
/ /

# Import "ViewLabel.h"

@ Implementation ViewLabel

id ) initWithFrame : ( CGRect ) frame { - (Id) initWithFrame: (CGRect) frame {

self = [ super initWithFrame : frame ] ) { if (self = [super initWithFrame: frame]) {
/ / Initialization code
}
return self;
}

/ /
/ / Our new initializer
/ /
id ) initWithLabelDefine : ( CGRect ) frame label : ( NSString * ) labelDefine { - (Id) initWithLabelDefine: (CGRect) frame label: ( NSString *) {labelDefine

self = [ super initWithFrame : frame ] ) { if (self = [super initWithFrame: frame]) {
/ / Here you can decide how to behave, I for
/ / I got the same frame security in the past
/ / Input but I reset the x and y
; frame.origin.x = 0;
; frame.origin.y = 0;
UILabel alloc ] initWithFrame : frame ] ; internalLabel = [[UILabel alloc] initWithFrame: frame];
internalLabel.text = labelDefine;
internalLabel ] ; [Self addSubview: internalLabel];
; [InternalLabel release];
}
return self;
}

void ) drawRect : ( CGRect ) rect { - (Void) drawRect: (CGRect) rect {
/ / Drawing code
}

void ) dealloc { - (Void) dealloc {
; [Super dealloc];
}

@ End

At the time of the creation of our new class, we could use:

1
[ [ ViewLabel alloc ] initWithLabelDefine : CGRectMake ( 0 , 0 , 320 , 80 ) label : @ "Ciao" ] ; ViewLabel * vl = [[ViewLabel alloc] initWithLabelDefine: CGRectMake (0, 0, 320, 80) label: @ "Hello"];

More ...

iPhone SecondApp: Guess the number - Part 2

As mentioned FirstApp iPhone: Guess the number - Part 1 we see how to make an application for the Apple iPhone without using Interface Builder! Indeed, we will eliminate physical files created by Interface Builder wizard Xcode. At the end of this post, then, we have an application identical in all respects, to those built in the first part, with the difference that we will achieve all our visual components, including the Main Window, for complete code.

The application already made, if you just want to download, is available on my Google Code repository:

I would like to point out immediately as the ZIP of this example weighs less than the last time! :)

We create the project

We start by creating our project SecondApp (to distinguish it from FirstApp), although this time choose Window-based Application:

newproject

Now let's delete everything related to Interface Builder. : eliminatelo anche dal file system, quindi selezionate Also Move to Trash . Eliminate the file MainWindow.xib , located in the folder Resources , remove it from the file system also, and then select Also Move to Trash. nella casella Main nib file base name : Select then the file SecondoApp-info.plist and delete the reference to the MainWindow nib file in the Main box based name:

deleteib

At this point we no longer have any Window, at least through Interface Builder. , e modifichiamo la funzione main() in questo modo: So we open the file main.m , situated in Other Sources , and modify the function main() like so:

1
2
3
4
5
6
7
8
9
10
11
int argc, char * argv [ ] ) { int main (int argc, char * argv []) {

pool = [ [ NSAutoreleasePool alloc ] init ] ; NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc] init];

/ / By removing the file. XIB we lost tracking
/ / The app delegate and then pass it to "hand"
UIApplicationMain ( argc, argv, nil , @ "SecondAppAppDelegate" ) ; int retVal = UIApplicationMain (argc, argv, nil, @ "SecondAppAppDelegate");

; [Pool release];
return retVal;
}

Open SecondAppAppDelegate.me then we create the main Window via code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void ) applicationDidFinishLaunching : ( UIApplication * ) application { - (Void) applicationDidFinishLaunching: (UIApplication *) application {

/ / Get the screen size (320, 480)
UIScreen mainScreen ] applicationFrame ] ; CGRect windowRect = [[UIScreen mainScreen] applicationFrame];
/ / Create a window - as we did bye bye to Interface Builder
[ [ UIWindow alloc ] initWithFrame : windowRect ] ; UIWindow mainWindow * = [[UIWindow alloc] initWithFrame: windowRect];
/ / Set the background of the Window to yellow, to differentiate
/ / The previous application FirstApp
[ UIColor yellowColor ] ] ; [MainWindow setBackgroundColor: [UIColor yellowColor]];

mainWindow ] ; [Self setWindow: mainWindow];
; [Window makeKeyAndVisible];

; [MainWindow release];
}

You can already test the application, if a window appears yellow you did everything right!

, necessario solo se si usa Interface Builder. In the file SecondAppAppDelegate.h we can eliminate IBOutlet , only needed if you use Interface Builder. Also add here that our global variables, the last time we had entered in the Controller. Then modify the file SecondAppAppDelegate.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Import <UIKit/UIKit.h>

NSObject <UIApplicationDelegate> { @ Interface SecondAppAppDelegate: NSObject {<UIApplicationDelegate>
UIWindow * window;

UITextField * number;
UIButton * button;
int numeroACaso;
}

nonatomic, retain ) UIWindow * window; @ Property (nonatomic, retain) UIWindow * window;

void ) controllaNumero; - (Void) getNumber;

@ End

, in quanto non stiamo usando Interface Builder. Also in this case we have prepared the method definition controllaNumero , as the last time, but we have eliminated the indication IBAction , since we are not using Interface Builder.

We build the interface code

It 's time to create code using all components of our interface. ed inseriamo il seguente codice: We return in the file SecondAppAppDelegate.m , posizioniamoci before [mainWindow release] and insert the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/ / Create the title bar
[ [ UINavigationBar alloc ] initWithFrame : CGRectMake ( 0.0 , 0.0 , 320.0 , 44.0 ) ] ; UINavigationBar myNavigationBar * = [[UINavigationBar alloc] initWithFrame: CGRectMake (0.0, 0.0, 320.0, 44.0)];
myNavigationBar.barStyle = UIBarStyleDefault;
[ [ UINavigationItem alloc ] initWithTitle : @ "Indovina un numero" ] ; UINavigationItem navigationItem * = [[UINavigationItem alloc] initWithTitle: @ "Guess a number"];
navigationItem animated : NO ] ; [MyNavigationBar pushNavigationItem: navigationItem animated: NO];
myNavigationBar ] ; [Window addSubview: myNavigationBar];

/ / Create the label
[ [ UILabel alloc ] initWithFrame : CGRectMake ( 10 , 50 , 300 , 80 ) ] ; UILabel myLabel * = [[UILabel alloc] initWithFrame: CGRectMake (10, 50, 300, 80)];
UIColor clearColor ] ; myLabel.backgroundColor = [UIColor clearColor];
; myLabel.numberOfLines = 2;
"iPhone ha pensato un numero da 1 a 10, prova ad indovinarlo?" ; myLabel.text = @ "iPhone has designed a number from 1 to 10, try to guess?"
myLabel ] ; [Window addSubview: myLabel];

/ / Create text input
UITextField alloc ] initWithFrame : CGRectMake ( 10 , 120 , 300 , 30 ) ] ; number = [[UITextField alloc] initWithFrame: CGRectMake (10, 120, 300, 30)];
numero.borderStyle = UITextBorderStyleRoundedRect;
numero.textAlignment = UITextAlignmentCenter;
numero.keyboardType = UIKeyboardTypeNumberPad;
"Inserisci il numero" ; numero.placeholder = @ "Enter number";
numero ] ; [Window addSubview: number];

/ / Create the button
UIButton buttonWithType : UIButtonTypeRoundedRect ] ; button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
10 , 180 , 300 , 30 ) ; bottone.frame = CGRectMake (10, 180, 300, 30);
@ "Premi qui" forState : UIControlStateNormal ] ; [Button setTitle: @ "Click here" forState: UIControlStateNormal];
self action : @selector ( controllaNumero ) forControlEvents : UIControlEventTouchUpInside ] ; [AddTarget button: self action: @ selector (getNumber) forControlEvents: UIControlEventTouchUpInside];
bottone ] ; [Window addSubview: button];

; [MyLabel release];
; [NavigationItem release];
; [MyNavigationBar release];

della scorsa volta, subito dopo il [mainWindow release]; inseriamo: Since the ' applicationDidFinishLaunching corresponds to viewDidLoad as last time, right after the [mainWindow release]; insert:

1
+ arc4random ( ) % 10 ; numeroACaso = 1 + arc4random ()% 10;

Now we can do is implement the method controllaNumero , which will be identical (apart from the prototype) to the one used last time:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void ) controllaNumero { - (Void) {getNumber
"Premuto bottone di controlla numero" ) ; NSLog (@ "Pressed button control number");
[ numero.text integerValue ] ; numeroInserito int = [numero.text integerValue];
message; NSString * message;
"Il numero inserito è %d" , numeroInserito ) ; NSLog (@ "The number entered is% d", numeroInserito);
numeroInserito <numeroACaso ) { if (numeroInserito <numeroACaso) {
"Troppo basso..." ; message = @ "too low ...";
( numeroInserito> numeroACaso ) { } Else if (numeroInserito> numeroACaso) {
"Troppo alto..." ; message = @ "Too high ...";
( numeroInserito == numeroACaso ) { } Else if (numeroInserito numeroACaso ==) {
"Bravo hai indovinato" ; message = @ "Bravo you guessed it";
+ arc4random ( ) % 10 ; numeroACaso = 1 + arc4random ()% 10;
"Numero pensato %d" , numeroACaso ) ; NSLog (@ "Number% d thought," numeroACaso);
}
[ [ UIAlertView alloc ] UIAlertView alertMessaggio * = [[UIAlertView alloc]
"Responso" initWithTitle: @ "Response"
message: message
delegate: nil
"OK" cancelButtonTitle: @ "OK"
] ; otherButtonTitles: nil];
; [AlertMessaggio show];
; [AlertMessaggio release];
"" ; numero.text @ = "";
}

We're done!

Conclusions and considerations

, proprio perchè volevo lasciarlo il più semplice e snello possibile e, anche, per dimostrare che non sono elementi sempre necessari. This example does not make direct use of a UIView or UIViewController , because I wanted to leave it as simple and streamlined as possible and, also, to show that the elements are not always necessary. However, inserting objects directly in the window can have some sense in this example and other sporadic contexts. portano comunque benefici in tantissimi altri casi, ein alcuni sono praticamente indispensabili; come avremo modo di vedere in futuro. The use of UIView and UIViewController still bring benefits in many other cases, andin some are almost indispensable, as we will see in the future.

More ...

XCode: organize your code with the # pragma mark

XCode is a very powerful and versatile and provides the programmer with many useful features and simple to use. When writing complex code, or at least articulated, it becomes important to organize your code so you do not waste time searching functions scattered in long lines of code. After commenting, the first and most important thing to do, environment XCode provides guidelines (nice) to improve the usability and legginilità within the development. One of these is the directive #pragma mark that becomes very useful in organizing groups of the code and methods.

In the picture below you can see the code part of my project PragmaTest :

pragmamark-1

The top dropdown menu allows you to list all the methods of our class. Now if we plug our method over the directive #pragma mark which has a syntax:

1
# Pragma mark {label}

We obtain:

pragmamark-2

The first #pragma mark with a hyphen (-) inserts a separator line. The second is a text (label) to taste. You can add the following statement where you want, organizing the code as you see fit. In addition you can also insert multiple rows pragma, type:

1
2
3
4
5
6
# Pragma mark -
# Pragma mark / **
# Pragma mark * Using the pragma
# Pragma mark * on multiple lines of code
# Pragma mark * /
void ) mioMetodo { } - (Void) myMethod {}

More ...

Very short trick: WordPress, highlight the results of a search

There are several plugins for WordPress that allow to improve the features and performance of the function "Search". Almost all the plugins available are useful and attractive feature highlight search terms in the search results. If you're not going to appensantire further your WordPress installation, but do not want to give up everything to a similar feature, here's how to easily implement a "highlights" of termii search results.

More ...

Very short trick: WordPress, exclude pages and search by post

The internal engine of WordPress exploited for the search (without the addition of the standard plug-in) makes a low-level classical query_post() . The search is dapperttutto, without exclusion. This feature can be controlled, however, allowing, for example, to perform a search on all but in particular categories, pages or post.

More ...

Apple iPhone: The First Release 1.2

thefirst12claim

appstore-60

Available on the AppStore update to the 1.2 release of thefirst . Here are the release notes:

  • Fully revised throughout the game's graphics, which is now considerably more comfortable
  • Compatible with Apple iPhone 3.0
  • Added sound effects
  • Revised and improved the navigability of the game and instructions

More ...



Stop SOPA