Jul 18, 2008

(Objective-c) Comment récupérer le chemin d'un fichier dans le projet sous XCode

En Objective-C, nous devons avoir le chemin absolue pour lire unfichier, même ce fichier est dans le meme répertoire des fichiers sources. Mais pour une application mobile, cette manipulation ne marche plus, parce que l'appareil mobile ne possède pas de même structure de fichier par rapport les ordinateur.

Une solution pour résoudre ce problème, mettre ce fichier dans la "Resource" de XCode, n'oublie pas cocher la case de le copier. Pour récupérer le chemin vers ce fichier:

NSString* designFilePath = [[NSBundle mainBundle] pathForResource:@"design" ofType:@"xml"];

Le fichier est "design.xml".

(Objective-c) How to realize multiple inheritance

The Objective-c is single inheritance, but using Protocol allows to implements multiple class's methods, just like Interface in JAVA.

Here is an example :

@protocol MouseListener
-(BOOL) mousePressed;
-(BOOL) mouseClicked;
@end
@protocol KeyboardListener
-(BOOL) keyPressed;
@end
@interface Toto : NSObject
{
...
}
@end

So in the implementation of Toto we could override these listener's methods.

Jul 16, 2008

(IPhone) How to catch a button's click event

Here is an example:

//ViewBasedAppDelegate.h
#import

@class ViewBasedViewController;

@interface ViewBasedAppDelegate : NSObject {
UIWindow *window;

}

@property (nonatomic, retain) UIWindow *window;
-(void) catchButton;

@end

//ViewBasedAppDelegate.m
#import "ViewBasedAppDelegate.h"

@implementation ViewBasedAppDelegate
@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10.0, 90.0, 160.0, 40.0);
[button setTitle:@"Button" forState:UIControlStateNormal];
[button addTarget:(id)self action:@selector(catchButton)
forControlEvents:UIControlEventTouchUpInside];
[window addSubview:button];
[window makeKeyAndVisible];

}

- (void)catchButton{
NSLog(@"button pressed");

}

- (void)dealloc {
[window release];
[super dealloc];
}

@end

Jul 15, 2008

(IPhone) How to use NSXmlDocument in the IPhone's application

The sdk final didn't included NSXmlDocument, there is only NSXmlParser, the reason is the weight problem, NSXmlParser is lignter.

But in some cases, the Dom is more userful than SAX, with some google research, i found the API of TouchXml, http://code.google.com/p/touchcode/.

It solved the problem to use NSXmlDocument in the applicatons. Don't forget to check out all source files and add 2 parameters in project.

OTHER LINKER FLAGS = -lxml2
HEADER SEARCH PATHS = /usr/include/libxml2

Jul 7, 2008

(IPhone) Dev on IPhone started

Today i put my feet on the IPhone world, i'm so exciting. So the dev started with reading docs and sample codes.