Determine PickerView Value After IBAction is pressed - iphone

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]];

Related

Xcode Point System Iphone Trivia Game

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];
}

how to importing the contacts from the simulator to my app table view?

one more question
i need to import the contacts from the i phone simulator to my app table view and i should display the details of each contact by clicking the contact from the table view is there any possibility? if it is there please give me
declare in declaration.h file..
- (void) showAlertWithTitle:(NSString *)title message:(NSString *)message;
and in declaration.m file use like bellow...
- (void) showAlertWithTitle:(NSString *)title message:(NSString *)message {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
use like bellow..
if ([firstName.text length]==0){
[self showAlertWithTitle:#"Empty Field" mesage:#"Please Enter First Name"];
} else if([sndname.text length]==0){
[self showAlertWithTitle:#"Empty Field" mesage:#"Please Enter Second Name"];
}

How to show a UIAlertView upon selection of an item in UIPickerView

In my picker view i have a "Custom" option which should popup a UIAlertView for the user to enter a new value, the value is saved in the plist source of the picker for future reference. xxxEditingDidBegin is being called repeatedly (never ending).
I presume its because my UIAlertView is triggering the picker to close.
How should I have done this?
- (IBAction)serviceTypeFieldEditingDidEnd:(UITextField *)sender
{
UIPickerView *picker = [sender.inputView.subviews objectAtIndex:0];
NSString *selText = [serviceTypeArray objectAtIndex: [picker selectedRowInComponent:0]];
sender.text = selText;
if (NSOrderedSame==[selText compare:#"Custom"])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
}
Implement method like
- (IBAction)doSelectDate:(UIDatePicker *)sender
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Hi" message:#"AlertView is shwoing" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
and connect above method with ValueChanged Event of UIPickerView;
I fixed it like this
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
NSString *rowString = [serviceTypeArray objectAtIndex:row];
if ([rowString compare:#"Custom"] == NSOrderedSame)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Custom Role"
message:#"Enter Role Title"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
[alertView show];
}
else
{
_serviceType.text = rowString;
}
}

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;
}