Hi Chaps
Over the last week or so I've had my head stuck in a couple of Objective-C / iOS Development books. My usual way of learning is read a couple of chapters and then complete the included examples, but try to build on them and maybe go a step further - which I'm sure is how many other people tend to learn best.
Because of work commitments etc, I've not really managed to do too much, but though as I haven't got much to do today - I'd have a lovely day of learning. So, I've read through a few chapters, got an a basic understanding and started to go through the examples. Like I mentioned earlier, I find the best way to get this stuff to stick in my head if to read the book, and then kind of abandon it and try to replicate the examples that I've read through, but using the SDK reference material and Google to work out any problems.
I could do with anyone just checking over my code and just giving there opinion - have I missed something? Is there an easier way to achieve something? I'm just after pointers as I go along really.
The example is a really basic question and answer application - you press a button to get a question and press another button to get an answer.
Here's the code:
Thanks in advance, Chaps.
Martin
Over the last week or so I've had my head stuck in a couple of Objective-C / iOS Development books. My usual way of learning is read a couple of chapters and then complete the included examples, but try to build on them and maybe go a step further - which I'm sure is how many other people tend to learn best.
Because of work commitments etc, I've not really managed to do too much, but though as I haven't got much to do today - I'd have a lovely day of learning. So, I've read through a few chapters, got an a basic understanding and started to go through the examples. Like I mentioned earlier, I find the best way to get this stuff to stick in my head if to read the book, and then kind of abandon it and try to replicate the examples that I've read through, but using the SDK reference material and Google to work out any problems.
I could do with anyone just checking over my code and just giving there opinion - have I missed something? Is there an easier way to achieve something? I'm just after pointers as I go along really.
The example is a really basic question and answer application - you press a button to get a question and press another button to get an answer.
Here's the code:
Code:
########## --- QuizModel.h --- ##########
@interface QuizModel : NSObject {
NSMutableArray *questions;
NSMutableArray *answers;
int questionsDone;
}
- (NSInteger)nextQA;
- (NSString *)getQuestion;
- (NSString *)getAnswer;
- (id)init;
@end
########## --- QuizModel.m --- ##########
#import "QuizModel.h"
@implementation QuizModel
- (NSString *) getQuestion {
return [questions objectAtIndex:[self nextQA]];
}
- (NSString *) getAnswer {
return [answers objectAtIndex:questionsDone-1];
}
- (int) nextQA {
if (questionsDone == [questions count]) {
questionsDone = 0;
}
return questionsDone++;
}
- (id) init {
[super init];
questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];
[questions addObject:@"Question 1"];
[answers addObject:@"Answer 1"];
[questions addObject:@"Question 2"];
[answers addObject:@"Answer 2"];
[questions addObject:@"Question 3"];
[answers addObject:@"Answer 3"];
return self;
}
@end
Code:
########## --- QuizViewController.h --- ##########
#import <UIKit/UIKit.h>
#import "QuizModel.h"
@interface QuizViewController : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UILabel *questionsLabel;
IBOutlet UILabel *answersLabel;
QuizModel *quizObject;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
- (IBAction)showQuestion;
- (IBAction)showAnswer;
@end
########## --- QuizViewController.h --- ##########
#import " QuizViewController.h"
@implementation QuizViewController
@synthesize window;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
quizObject = [[QuizModel alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[window release];
[super dealloc];
}
- (IBAction)showQuestion {
[questionsLabel setText:(NSString *)[quizObject getQuestion]];
}
- (IBAction)showAnswer {
[answersLabel setText:(NSString *)[quizObject getAnswer]];
}
@end
Thanks in advance, Chaps.
Martin