how to Apply upgrades (inappPurchage )for buttons? - ios

I have done a photoEditorApp, which is in AppStore. Now i want to apply InApppurchage for some Buttons only. In my App overlays button is there when we click that button we are getting 7 buttons in which 3 buttons for free remaining 4 buttons for purchase .for that i have written code like this .but its not working don't know where i did mistake so please anyone suggest how to do UIScrollView *scrollView;
-(void)createScrolling2
{
previousButtonTag=1000;
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 4, self.overlaysView.frame.size.width, 106)];
int x =0.5;
for (int i = 0; i<8; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(x, 0, 100, 100);
button.layer.borderWidth=0.5;
button.titleLabel.font=[UIFont boldSystemFontOfSize:12];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.layer.borderColor=[[UIColor lightGrayColor]CGColor];
button.tag = i;
if (subButtonTag == 101) {
[button setBackgroundImage:[self.fireworksArray objectAtIndex:i] forState:UIControlStateNormal];
}
else if (subButtonTag == 102){
[button setBackgroundImage:[self.flowersArray objectAtIndex:i] forState:UIControlStateNormal];
}
else if (subButtonTag == 103){
[button setBackgroundImage:[self.loveArray objectAtIndex:i] forState:UIControlStateNormal];
}
else if (subButtonTag == 104){
[button setBackgroundImage:[self.rainbowArray objectAtIndex:i] forState:UIControlStateNormal];
}
int previousButtonTag;
-(void)OverLayMethod:(UIButton*)sender{
UIButton *selectButton = (UIButton*)sender;
NSLog(#"hi %d",sender.tag);
previousButtonTag=selectButton.tag;
NSLog(#"hi %d",sender.tag);
UIImage *selectImage;
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone){
if (subButtonTag == 101) {
selectImage = [self.fireworksArray objectAtIndex:sender.tag];
NSLog(#"selectImage = %#",selectImage);
self.overlayImgView.image = selectImage;
}
else if (subButtonTag == 102){
selectImage = [self.flowersArray objectAtIndex:sender.tag];
NSLog(#"selectImage = %#",selectImage);
self.overlayImgView.image = selectImage;
}
else if (subButtonTag == 103){
selectImage = [self.loveArray objectAtIndex:sender.tag];
NSLog(#"selectImage = %#",selectImage);
self.overlayImgView.image = selectImage;
}
else if (subButtonTag == 104){
selectImage = [self.rainbowArray objectAtIndex:sender.tag];
NSLog(#"selectImage = %#",selectImage);
self.overlayImgView.image = selectImage;
}
- (IBAction)buyPackClicked:(id)sender
{
if (![demoPurchase restorePurchase])
{
UIAlertView *settingsAlert = [[UIAlertView alloc] initWithTitle:#"Allow Purchases" message:#"You must first enable In-App Purchase in your iOS Settings before restoring a previous purchase." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[settingsAlert show];
}
}
- (IBAction)restorePreviousClicked:(id)sender
{
if (demoPurchase.validProduct != nil)
{
if (![demoPurchase purchaseProduct:demoPurchase.validProduct])
{
UIAlertView *settingsAlert = [[UIAlertView alloc] initWithTitle:#"Allow Purchases" message:#"You must first enable In-App Purchase in your iOS Settings before making this purchase." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[settingsAlert show];
}
}
}
-(void) requestedProduct:(EBPurchase*)ebp identifier:(NSString*)productId name:(NSString*)productName price:(NSString*)productPrice description:(NSString*)productDescription
{
SKProduct *product = ebp.validProduct; // epb is the EBPurchase object
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:product.priceLocale];
NSString *formattedPrice = [numberFormatter stringFromNumber:product.price];
NSLog(#"ViewController requestedProduct %#",productPrice);
if (productPrice != nil)
{
[buyPack setTitle:[#"Buy Pack item " stringByAppendingString:productPrice] forState:UIControlStateNormal];
buyPack.enabled = YES; // Enable buy button.
}
else {
buyPack.enabled = NO; // Ensure buy button stays disabled.
[buyPack setTitle:#"Buy Pack item" forState:UIControlStateNormal];
UIAlertView *unavailAlert = [[UIAlertView alloc] initWithTitle:#"Not Available" message:#"This In-App Purchase item is not available in the App Store at this time. Please try again later." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[unavailAlert show];
}
}
-(void) successfulPurchase:(EBPurchase*)ebp restored:(bool)isRestore identifier:(NSString*)productId receipt:(NSData*)transactionReceipt
{
NSLog(#"ViewController successfulPurchase");
if (!isPurchased)
{
isPurchased = YES;
NSString *alertMessage;
if (isRestore) {
alertMessage = #"Your purchase was restored and the Game Levels Pack is now unlocked for your enjoyment!";
} else {
alertMessage = #"Your purchase was successful and the Game Levels Pack is now unlocked for your enjoyment!";
}
UIAlertView *updatedAlert = [[UIAlertView alloc] initWithTitle:#"Thank You!" message:alertMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[updatedAlert show];
}
}
-(void) failedPurchase:(EBPurchase*)ebp error:(NSInteger)errorCode message:(NSString*)errorMessage
{
NSLog(#"ViewController failedPurchase");
UIAlertView *failedAlert = [[UIAlertView alloc] initWithTitle:#"Purchase Stopped" message:#"Either you cancelled the request or Apple reported a transaction error. Please try again later, or contact the app's customer support for assistance." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[failedAlert show];
}
-(void) incompleteRestore:(EBPurchase*)ebp
{
NSLog(#"ViewController incompleteRestore");
UIAlertView *restoreAlert = [[UIAlertView alloc] initWithTitle:#"Restore Issue" message:#"A prior purchase transaction could not be found. To restore the purchased product, tap the Buy button. Paid customers will NOT be charged again, but the purchase will be restored." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[restoreAlert show];
}
-(void) failedRestore:(EBPurchase*)ebp error:(NSInteger)errorCode message:(NSString*)errorMessage
{
NSLog(#"ViewController failedRestore");
UIAlertView *failedAlert = [[UIAlertView alloc] initWithTitle:#"Restore Stopped" message:#"Either you cancelled the request or your prior purchase could not be restored. Please try again later, or contact the app's customer support for assistance." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[failedAlert show];
}
Here when I want to click any button in that 4 buttons getting alert for buy diaplying cost.

Related

executing a popup from another popup button in ios

I am new in IOS. When I'm clicking on the following Login Button:
I'm using this code to generate a popup:
- (UIView *)createLoginView
{
UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)];
UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 280, 50)];
email.tag=420;
email.layer.cornerRadius = 5;
[email setTag:99];
[email setTextAlignment:NSTextAlignmentCenter];
[email setKeyboardType:UIKeyboardTypeEmailAddress];
[email setPlaceholder:#"Email"];
[email setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[email setReturnKeyType:UIReturnKeyNext];
[email setAutocorrectionType:UITextAutocorrectionTypeNo];
UITextField *pass = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, 280, 50)];
pass.layer.cornerRadius = 5;
[pass setTag:100];
[pass setTextAlignment:NSTextAlignmentCenter];
[pass setSecureTextEntry:YES];
[pass setPlaceholder:#"Password"];
[pass setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[pass setReturnKeyType:UIReturnKeyGo];
[alertView addSubview:email];
[alertView addSubview:pass];
return alertView;
}
The login popup is working properly but when I click on the 'Forget?' button of the popup, the forgot password popup does not show up. Here is the code for forget password popup.
- (UIView *)createForgotPasswordView
{
UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 130)];
UITextField *email = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 280, 50)];
email.layer.cornerRadius = 5;
[email setTag:22];
[email setTextAlignment:NSTextAlignmentCenter];
[email setKeyboardType:UIKeyboardTypeEmailAddress];
[email setPlaceholder:#"Email"];
[email setBackgroundColor:[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1.0]];
[email setReturnKeyType:UIReturnKeyNext];
[email setAutocorrectionType:UITextAutocorrectionTypeNo];
[alertView addSubview:email];
return alertView;
}
And I'm using it like :
- (IBAction)LogInClick:(UIButton *)sender {
IOS7AlertView *applyMsg = [[IOS7AlertView alloc] init];
[applyMsg setContainerView:[self createLoginView]];
[applyMsg setButtonTitles:[NSMutableArray arrayWithObjects:#"Forgot?", #"Login", nil]];
[applyMsg setOnButtonTouchUpInside:^(IOS7AlertView *alertView, int buttonIndex) {
if(buttonIndex==0)
{
[alertView close];
NSLog(#"Forgot Section");
IOS7AlertView *applyPop = [[IOS7AlertView alloc] init];
[applyPop setParentView:[self createForgotPasswordView]];
[applyPop setButtonTitles:[NSMutableArray arrayWithObjects:#"Next", #"Cancel", nil]];
}
}];
But the problem is the next popup does not show up? What is the problem? Please help.
already UIalertview hierarchy has email and password options.
try this and this is native
- (IBAction)LogInClick:(UIButton *)sender {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Login"
message:nil
delegate:self
cancelButtonTitle:#"Signup"
otherButtonTitles:#"Login",#"Forgot password", nil];
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
[[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:0]setPlaceholder:#"Email address"];
[[alert textFieldAtIndex:1]setPlaceholder:#"Password"];
alert.tag=10;
[alert show];
}
and the alert view delegate method is
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag==65)
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Login"
message:nil
delegate:self
cancelButtonTitle:#"Signup"
otherButtonTitles:#"Login",#"Forgot password", nil];
alert.alertViewStyle=UIAlertViewStyleLoginAndPasswordInput;
[[alert textFieldAtIndex:0]setKeyboardType:UIKeyboardTypeEmailAddress];
[[alert textFieldAtIndex:0]setPlaceholder:#"Email address"];
[[alert textFieldAtIndex:1]setPlaceholder:#"Password"];
alert.tag=10;
[alert show];
}
if (alertView.tag==10)
{
if (buttonIndex == 0) {
NSLog(#"signup");
//Navigate to Signup page
}
else if (buttonIndex == 2) {
NSLog(#"Forgot password");
// //Navigate to Forgot password page
}
else if (buttonIndex == 1) {
NSLog(#"YES");
NSString *usernameInput=[alertView textFieldAtIndex:0].text;
NSString *passwordInput=[alertView textFieldAtIndex:1].text;
//pass the email id , password
NSLog(#"1 %#", usernameInput);
NSLog(#"2 %#", passwordInput);
NSString *emailReg = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailReg];
// COMMENT: check if input information is valid or not
if([usernameInput isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert!!!" message:#"You must fill in Email address" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
alert.tag=65;
[alert show];
}
else if([emailTest evaluateWithObject:usernameInput] == NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert!!" message:#"Please Enter Valid Email Address." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.tag=65;
[alert show];
}
else if([passwordInput isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Alert!!!" message:#"You must fill in Password" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
alert.tag=65;
[alert show];
}
else
{
//pass teh data to server or login process
}
}
}

iOS Form Validation Error via if-else Condition Does Not Work Properly

I've written a simple app which validates user input (whether NULL or longer than a define length). It should return validation error messages when validation fails and otherwise, redirect to another page.
However, the app only returns the messge for the first condition (Username is Empty) for all scenarions. (Such as username is filled and password is empty, etc.)
m file:
- (IBAction)doLogin {
if(uname.text==NULL) {
UIAlertView *err1 = [[UIAlertView alloc]
initWithTitle:#"Required field!" message:#"Username is empty." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err1 show];
NSLog(#"%#",uname.text);
}
else if(passw.text==NULL) {
UIAlertView *err2 = [[UIAlertView alloc]
initWithTitle:#"Required field!" message:#"Password is empty." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err2 show];
NSLog(#"%#",passw.text);
}
else if (uname.text.length < 6)
{
UIAlertView *err3 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a username longer than 6 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err3 show];
NSLog(#"%#",uname.text);
}
else if (uname.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",uname.text);
}
else {
/*UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Thank you" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:#"OK", nil];
[alert show];*/
UIViewController* flipViewController = [[UIViewController alloc] initWithNibName:#"flip" bundle:[NSBundle mainBundle]];
[self.view addSubview:flipViewController.view];
}
An alternative to karthika (but using similar structure) this will provide feedback on the entire form in a single message. Perhaps a little more user friendly and certainly reduces negative user interaction.
-(BOOL)isFormDataValid{
NSMutableArray *errorMessages = [[NSMutableArray alloc] init];
if([self.emailTextField.text isEqualToString:#""])
{
[errorMessages addObject:NSLocalizedString(#"Please enter email",nil)];
}
if([self.passwordTextField.text isEqualToString:#""])
{
[errorMessages addObject:NSLocalizedString(#"Please enter password",nil)];
}
if ([errorMessages count]) {
NSString * msgs = [errorMessages componentsJoinedByString:#"\n"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Whoops!",nil) message:msgs delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return NO;
} else {
return YES;
}
}
-(BOOL)isFormDataValid{
NSString *errorMessage = nil;
UITextField *errorField;
if([nameTextField.text isEqualToString:#""])
{
errorMessage = #"Please enter username";
errorField = nameTextField;
}
else if([[nameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
{
errorMessage = #"white spaces not allowed";
errorField = nameTextField;
}
else if([passwordTextField.text isEqualToString:#""])
{
errorMessage = #"Please enter password";
errorField = passwordTextField;
}
else if([[passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0)
{
errorMessage = #"white spaces not allowed";
errorField = passwordTextField;
}
if (errorMessage) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Failed!" message:errorMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[errorField becomeFirstResponder];
return NO;
}else{
return YES;
}
}
Apart from the fact that you do
else if (uname.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",uname.text);
}
instead of
else if (passw.text.length < 8)
{
UIAlertView *err4 = [[UIAlertView alloc]
initWithTitle:#"Invalid!" message:#"Enter a password longer than 8 chars." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[err4 show];
NSLog(#"%#",passw.text);
}
Your code should work just fine.
Also, bear in mind that a text field's text won't be nil, it will just be an empty string (lenght == 0), unless you explicitly set it to nil.

iOS: Simplified UIAlertView Code So It Can Be Used Over And Over Again

this is my second day on learning XCode, so I'm totally newbie here and I have this .m code :
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[_fieldEmail setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:14]];
[_fieldPassword setFont:[UIFont fontWithName:#"ABeeZee-Regular" size:14]];
[_titleLogin setFont:[UIFont fontWithName:#"Raleway-ExtraLight" size:28]];
UIView *fieldEmail = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldEmail.leftViewMode = UITextFieldViewModeAlways;
_fieldEmail.leftView = fieldEmail;
UIView *fieldPassword = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
_fieldPassword.leftViewMode = UITextFieldViewModeAlways;
_fieldPassword.leftView = fieldPassword;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)validateEmail:(NSString *)emailStr {
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:emailStr];
}
- (IBAction)buttonRegister {
_labelOutput.text = #"Register?";
}
- (IBAction)buttonLogin {
if ([_fieldEmail.text length] > 0) {
if(![self validateEmail:[_fieldEmail text]]){
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Email Error"
message:#"That's not a valid email!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}else{
_labelOutput.text = #"Login?";
}
}else{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Please enter your email"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
}
- (IBAction)loginFacebook {
if ([_fieldPassword.text length] > 15) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Password Error"
message:#"Password must be less than 15 characters."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}else{
_labelOutput.text = #"Login with FB?";
}
}
- (IBAction)loginTwitter {
// _labelOutput.text = #"Login with Twitter ya?";
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Please enter your email correctly."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
#end
as you can see there, the code I used to display alert box is repeated. Is there any way to put this similar code somewhere up there so it can be called just by giving Title and Message as variable :
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Login Error"
message:#"Please enter your email correctly."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
In PHP world, this method named function, but I don't know what's name in Objective-C world. thank you very much.
To reduce your code why dont you create another method to show the AlertBox ?
- (void) alertWithTitle:(NSString *)title andMessage:(NSString *)msg
{
UIAlertView *message = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
}
Then simply call :
[self alertWithTitle:(YOUR TITLE) andMessage:(YOUR MESSAGE)];
You have written a handful of methods, have you considered just writing another one? E.g.
- (void) showAlertWithTitle:(NSString *)title andMessage:(NSString *)message
{
...
}
and then when you need an alert call this method, e.g.:
[self showAlertWithTitle:#"Login Error" andMessage:#"Please enter your email correctly."];
HTH

What should i do, when user cant send message to twitter

I got app, which can send twits.
I do it in this way:
- (IBAction)twitDream:(id)sender
{
if ([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweet =
[[TWTweetComposeViewController alloc] init];
if (dream.image != [UIImage imageNamed:#"blank-photo.png"])
[tweet addImage:dream.image];
NSString *twitMsg = [dreamField.text stringByAppendingString:#" send via Dreamer"];
[tweet setInitialText:twitMsg];
[self presentModalViewController:tweet animated:YES];
} else {
//can't tweet!
}
}
What should i do when [TWTweetComposeViewController canSendTweet] is equal to NO ? And when it is equal to NO ?
That is full solution code:
- (IBAction)twitDream:(id)sender
{
if ([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweet =
[[TWTweetComposeViewController alloc] init];
if (dream.image != [UIImage imageNamed:#"blank-photo.png"]) {[tweet addImage:dream.image];}
NSString *twitMsg = [dreamField.text stringByAppendingString:#" #Dreamer"];
[tweet setInitialText:twitMsg];
[self presentModalViewController:tweet animated:YES];
} else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}

How do I make an alert show how long it took to complete a certain amount of questions in xcode?

I want to show the user how long it took him or her to answer a certain amount of questions (for example 5) as an alert. I believe that I have the correct timer in place although I could be wrong. How would I finish the coding?
- (void)updateCounter:(NSTimer *)theTimer {
static int count = 0;
count += 1;
NSString *s = [[NSString alloc]
initWithFormat:#"%d", count];
self.timer.text = s;
[s release];
}
- (void)generate
{
int a = 1 + arc4random() % 12;
int b = 1 + arc4random() % 12;
int sum = a * b;
label.text = [NSString stringWithFormat: #"%d * %d = ", a, b];
label.tag = sum;
}
- (void)viewDidLoad
{NSLog(#"viewDidLoad");
[super viewDidLoad];
[self generate];
[self.answer becomeFirstResponder];
UIAlertView *alert;
{alert = [[UIAlertView alloc]
initWithTitle:#"Welcome to iCanMultiply!"
message:#"Tap 'Start' to start the game"
delegate: self
cancelButtonTitle:#"Start"
otherButtonTitles: nil];
}
[alert show];
[alert release];
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:#selector(updateCounter:)
userInfo:nil
repeats:YES];
}
- (IBAction)submit:(id)sender {
int num = [answer.text intValue];
UIAlertView *alert;
if (num == label.tag)
{
// answer was correct
alert = [[UIAlertView alloc]
initWithTitle:#"Correct"
message:#"Bet you can't do that twice!"
delegate:self
cancelButtonTitle:#"Next Question"
otherButtonTitles: nil];
// use the alert tag to mark that answer was correct
alert.tag = 1;
} else
{
// answer is incorrect
alert = [[UIAlertView alloc]
initWithTitle:#"Wrong!"
message:#"Sorry, try again..."
delegate:self
cancelButtonTitle:#"Try Again"
otherButtonTitles: nil];
}
// show and release the alert
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
[self generate];
answer.text = #"";
}
}
Store that value and then display it in an alert view.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message#"It took you %# seconds.",time delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
Let me know if the ,time works. I haven't tested this yet, but it should work.

Resources