Xcode Point System Iphone Trivia Game - iphone

I am a beginner at creating iPhone apps and am trying to incorporate a simple point system for an iphone trivia game. Here is an basic overview of the app:
http://i.stack.imgur.com/nC0CI.png
So basically what I'm trying to do is make the user type the answer in the textfield. After typing an answer, they would click the button "Answer". If the answer is correct, an alert would show up saying "Correct", adding 100. If incorrect, it would say "Incorrect", subtracting 100.
However here is the problem: when you first answer correctly or incorrectly, the number remains at 0. When you click the button a second time, then it would properly increase/decrease. How do you fix this?
Here is my .h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
NSString *stringpoint;
int point;
}
#property (nonatomic, retain) IBOutlet UITextField *textAnswer;
#property (nonatomic, retain) IBOutlet UILabel *pointlabel;
-(IBAction) checkAnswer;
#end
.m file
#synthesize textAnswer, pointlabel;
int point=0;
-(IBAction)checkAnswer {
pointlabel.text = [NSString stringWithFormat:#"%i",point];
if ([textAnswer.text isEqualToString:#"fruit"]) {
point=point+100;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Correct!"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Incorrect"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
point=point-100;
[alert show];
[alert release];
}
}
I am using Xcode 4.3.

You are updating the label that shows points before you calculate the new total, just need to move it to after:
-(IBAction)checkAnswer {
// don't do this here, too early
// pointlabel.text = [NSString stringWithFormat:#"%i",point];
if ([textAnswer.text isEqualToString:#"fruit"]) {
point=point+100;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Correct!"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Incorrect"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
point=point-100;
[alert show];
[alert release];
}
// now you've changed value of points above, so update the label
pointlabel.text = [NSString stringWithFormat:#"%i",point];
}

Related

Control the transition between two viewControllers

I am new to objective-c and have recently started making iOS applications in Xcode. I was making a login application that has two view controllers. It looks like this: http://imgur.com/W3nxMEG
Now my issue is that I was the app to go to second ViewController only if:
1) Text fields are filled
2) Passwords match
that's the code that I've written. I couldnt find any command that controls the transition between the viewControlled based on the certain constraints:
(IBAction)loginButton:(id)sender {
BOOL passMatch, emptyUser, emptyPass, emptyrePass;
passMatch = [password isEqualToString:reEnter];
emptyUser = [username isEqualToString:#""];
emptyPass = [password isEqualToString:#""];
emptyrePass = [reEnter isEqualToString:#""];
if (emptyPass || emptyUser || emptyrePass){
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"You must complete all the fields " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
else{
passMatch = [password isEqualToString:reEnter];
if (passMatch){
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:#"Success!" message:#"Passwords match " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[pass show];
} else{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"Passwords dont match " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
}
Shall be thankful if anyone can help me solve this
First make sure your UITextField and UIButton from your storyBoard are hooked with a property in LoginViewController.h
It'll look like this:
LoginViewController.h
#interface LoginViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *password;
#property (strong, nonatomic) IBOutlet UITextField *username;
// and so on..
// for your button
#property (strong, nonatomic) IBOutlet UIButton *loginButton;
#end
LoginViewController.m
#implementation LoginViewController
-(IBAction)loginButton:(id)sender
{
// you can enable/disable your button here also, by:
// UIButton *senderButton = (UIButton *)sender;
// senderButton.enabled = YES/NO;
// i'm just using .length because i trust numbers more that the #""
NSString *errorMessage;
if (self.username.text.length == 0)
errorMessage = #"username is required";
else if (self.password.text.length == 0)
errorMessage = #"password is required";
else if (self.reEnter.text.length == 0)
errorMessage = #"please confirm your password";
else if (self.password.text.length == 0 || self.username.text.length == 0 || self.reEnter.text.length == 0)
errorMessage = #"You must complete all the fields";
if (errorMessage.length > 0)
{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:errorMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
else
{
if ([self.username.text isEqual: #"youUsername"] && [self.password.text isEqual: #"youPassword"] && [self.password.text isEqual: self.reEnter.text])
{
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:#"Success!" message:#"Login successful" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[pass show];
//presentYourSecondViewController
//[self presentViewController:(UIViewController) animated:(BOOL) completion:nil];
//[self.navigationController pushViewController:(UIViewController)animated:(BOOL)];
}
else
{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"Login failed" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
}
/* Your code...
BOOL passMatch, emptyUser, emptyPass, emptyrePass;
passMatch = [password isEqualToString:reEnter];
emptyUser = [username isEqualToString:#""];
emptyPass = [password isEqualToString:#""];
emptyrePass = [reEnter isEqualToString:#""];
if (emptyPass || emptyUser || emptyrePass){
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"You must complete all the fields " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
else
{
passMatch = [password isEqualToString:reEnter];
if (passMatch){
UIAlertView *pass=[[UIAlertView alloc] initWithTitle:#"Success!" message:#"Passwords match " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[pass show];
} else{
UIAlertView *error=[[UIAlertView alloc] initWithTitle:#"Ooops!" message:#"Passwords dont match " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[error show];
}
}
*/
}
#end
There is nothing wrong with your condition i just over did it i guess.. Anyway hope i've help you Happy coding cheers..

Determine PickerView Value After IBAction is pressed

I have an iOS UiPicker working with my desired values and can detect when a new option is selected with the following code:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *aCompany = [arrayColour objectAtIndex:[pickerView selectedRowInComponent:0]];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Picked:"
message:aCompany
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
My question is how do I get the latest from the picker after pressing a button like this:
- (IBAction)DoneButton:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Picked:"
message:aCompany
delegate:nil
cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
Set up your pickerView as an IBOutlet (and link using IB) then you can access it outside of the didSelectRow.
#property (nonatomic) IBOutlet UIPickerView *myPickerView;
Then you can use the following line in DoneButton:
NSString *aCompany = [arrayColour objectAtIndex:[self.myPickerView selectedRowInComponent:0]];

Cannot show UIAlertView with a text field

This piece of code which is supposed to show an alert window with a text input:
self.alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];
Causes this error:
Thread 7: Program received signal: "EXC_BAD_ACCESS"
This is how self.alert is defined:
#interface MyClass : NSObject
{
UIAlertView *alert;
id <MyClassDelegate> __unsafe_unretained delegate;
}
#property (nonatomic, retain) UIAlertView *alert;
#property (unsafe_unretained) id <MyClassDelegate> delegate;
The problem is maybe because of the customize.
I do not know why, but appear to me that the problem is because of the use of threads + customize of your alert.
Can you try to show this alert on the main thread? What happen?
You probably get an error in this line:
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
What you need to do if yes, is perform this in the main thread.
- (void) yourMethod{
[self performSelectorOnMainThread:#selector(yourMethod2) withObject:nil waitUntilDone:NO];
}
- (void) yourMethod2{
self.alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];
}
Sorry to can't help you more than that, but I do not know exactly what happen, but I already read about issues when editing things to show, in other threads.
Hope it help you!
The EXC_BAD_ACCESS is caused by accessing a released object. To avoid this make your call to UIAlertView kind of modal:
Function body:
-(void)checkSaving
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Do you want to add these results to your database?"
message:#"\n\n"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Save", nil];
alert.alertViewStyle = UIAlertViewStyleDefault;
[alert show];
//this prevent the ARC to clean up :
NSRunLoop *rl = [NSRunLoop currentRunLoop];
NSDate *d;
d= (NSDate*)[d init];
while ([alert isVisible])
{
[rl runUntilDate:d];
}
}
Your choice result:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)//Save
{
//do something
}
if (buttonIndex == 0)//NO
{
//do something
}
}
Register the functions in the interface declaration:
#interface yourViewController ()
-(void)checkSaving
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//...
#end
To call:
[self checkSaving];
I wish this will help you.

iPhone SDK: How can you hide an alert box?

I am using the following code to show an alert box:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Info" message:#"My Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
Can you tell me how to hide the alert box again when the phone changes orientation?
First you have to save a reference to the alert in your interface.
#interface MyViewController : UIViewController {
UIAlertView *alert;
}
#property (nonatomic, retain) IBOutlet UIAlertView *alert;
when you create the alert you use
self.alert = [[[UIAlertView alloc] initWithTitle:#"Info" message:#"My Message" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] autorelease];
[alert show];
and then you have to add another method didRotateFromInterfaceOrientation:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[alert dismissWithClickedButtonIndex:[alert cancelButtonIndex] animated:YES];
self.alert = nil;
}

objective-c : iphone programming : Showing variables on UIAlert view

i want simply to show a double value on a uialert view it is possible?
Put the value into a formatted NSString and send that to the alertView:
NSString *messageString = [NSString stringWithFormat:"#number = %.2f", 42.0];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Title text" message:messageString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
How are you showing the UIAlertView now? Something like the following should work:
double myDouble = 12.6;
NSString *alertString = [NSString stringWithFormat:#"%g", myDouble];
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:alertString
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
[myAlert show];