Assigning UIPickerView value issue on device - iphone

I am facing this issue on device that assigning the selected row value of picker takes too much time to be shown on label while on simulator it works fine.
Below is the code:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
if (pickerView == rewardsName) {
NSString *selectedReward = [self.rewards objectAtIndex:row];
if ([reward1.text length]==0){
//set label 1
reward1.text = selectedReward;
}
if ([reward2.text length]==0){
//set label 2
reward2.text = selectedReward;
}
if([reward3.text length]==0){
//set label 3.
reward3.text = selectedReward;
}
}
else if(pickerView == selectNamazPicker){
aNumberString = [self.namazArray objectAtIndex:row];
NSLog(#"aNumberString:: %#", aNumberString);
}
}
Can anyone tell me what can be the issue ... I am unable to find one...
**
EDITED:
ISSUE RESOLVED:
**
For others who would face this issue.
I don't know whether it was creating the problem or not. But I did make these changes to my code.
In my .h file, previously I was just defining properties of UILabels (reward1, reward2 etc) with IBOutlet and not ivars. But then I defined ivars as IBOutlet UILabel reward1; etc and eliminated IBOutlet from properties then it started working fine.
Now the problem has resolved.

For others who would face this issue.
I don't know whether it was creating the problem or not. But I did make these changes to my code.
In my .h file, previously I was just defining properties of UILabels (reward1, reward2 etc) with IBOutlet and not ivars. But then I defined ivars as IBOutlet UILabel reward1; etc and eliminated IBOutlet from properties then it started working fine.
Now the problem has resolved.

Related

Saving an array object as a string within a picker view?

So I am working with a pickerview and need a string to equal one of several things that I have stored in an array. The pickerview code for the components looks like this.
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (row) {
case 0:
image.image = image1;
email = EmailArray[1];
break;
case 1:
image.image = image2;
email = EmailArray[3];
break;
case 2:
image.image = image3;
email = EmailArray[5];
break;
}
NSLog(#"%#", email)
}
This code works just fine as far as I can tell. The NSLog here returns the correct email every time. The user then presses a button that utilizes the "email" string. Here is the first part of that code.
- (IBAction)sendFeedback:(id)sender
{
NSLog(#"%#", email);
}
Only here the NSLog does not return the correct email. It just freezes the program and gives me a threading error that points at that NSLog. Am I not passing the array object to the string correctly?
If I change the code to this it works just fine.
switch (row) {
case 0:
image.image = image1;
email = #"testEmail#gmail.com";
break;
After this both NSLogs will display the test email.
Please help me figure out this problem. If you need more information just ask.
EDIT
Here is the beginning of my ViewController.h file.
#interface ViewController : UIViewController
<UIPickerViewDataSource, UIPickerViewDelegate, UIImagePickerControllerDelegate> {
NSMutableArray *EmailArray;
}
Here is where email is declared in the ViewController.h.
#implementation ViewController
NSString *email;
The error I get looks like this.
Thread 1:EXC_BAD_ACCESS(code=2, address=0x14)
You should not declared email as an instance var. An NSString must be declared as a property with copy modifier
#property (copy, nonatomic) NSString* email;
Your variable is not retained in memory, hence the crash.
You have two options
Add [email retain] after you assign it
Enable ARC and it will retain that variable for you.

iphone - Can't format label

I can't format a label, here's my code:
ct.text = [NSString stringWithFormat:#"%i:%i", currentTimeMin, currentTime];
so ct is the label and it won't display the value.
ct in the .h file: #property (nonatomic, retain) IBOutlet UILabel *ct;
I used (strong, nonatomic) as well but that didn't change anything
currentTimeMin is set to 0 and currentTime to self.audioPlayer.currentTime I think the problem could be there.
I have also tried to put ct.text = [NSString stringWithFormat:#"hey"]; didn't work either
Please help me! I am getting so sick of this. Thanks!
**EDIT**
And a weird thing is that I set up a label in another ViewController and then it worked but it's just in that single view that it won't work. Somehow, someway.
Judging from your comments and tests...
Either
(1) You are not referring to the label correctly. It should be self.ct
Or
(2) You have not wired up the label correctly. Please check it again. If you mean to assign content to a label.text but have not connected it to the property you will get a null result (as far as the program is concerned you have not alloc/init'ed the property).

deleting contents of a label - Objective C

I have a label which stores any data that is entered. it has a property and has been synthesized.
#property (strong, nonatomic) IBOutlet UILabel *memoryDisplay;
#synthesize memoryDisplay;
-(void)viewDidLoad
{
[super viewDidLoad];
view.hidden = YES;
}
The label is in a view which is hidden on load
The view has a property and has been synthesized
on the same view there is a button for clearing a label
It has an Action
.h
- (IBAction)clearMemory:(id)sender;
And
.m
- (IBAction)clearMemory:(id)sender
{
self.memoryDisplay.text = #"";
}
However, when ever I try to run the app it crashes and gives me this error
* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key clearMemory.'
* First throw call stack:
(0x13bc052 0x154dd0a 0x13bbf11 0x9b3032 0x924f7b 0x924eeb 0x93fd60 0x23291a 0x13bde1a 0x1327821 0x23146e 0xd8e2c 0xd93a9 0xd95cb 0x39a73 0x39ce2 0x39ea8 0x40d9a 0x11be6 0x128a6 0x21743 0x221f8 0x15aa9 0x12a6fa9 0x13901c5 0x12f5022 0x12f390a 0x12f2db4 0x12f2ccb 0x122a7 0x13a9b 0x1b28 0x1a85)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
I also tried to add some if statements to check for possible problems:
- (IBAction)clearMemory:(id)sender
{
if (!view.hidden) {
if ([memoryDisplay.text length] > 1)
{
self.memoryDisplay.text = #"";
}
}
Can anyone tell me what the problem might be?
If I take off everything got to do with the clear button it all works perfectly.
Thanks :)
Check all IB outlet bindings for broken links
Check to see if your memoryDisplay property is connected from the viewController to the UILabel in the nib/xib.
I couldn't do it the way I wanted, however I did find a workaround.
I placed a hidden, uneditable text box in the view which was empty and set the labels text to the text box whenever the clear button was pressed.

trying to edit uitableviewcell from delegate not working

I have this delegate method below that is setting a few fields in my tableview. What happens is when one of the tableview cells is pressed it loads a subview with a bunch more tableview cells when one is selected is pops the view from the viewcontrollerand loads the value that was selected into the parentviews cell that was initially selected.
This main cell effects what I am able to set in the second cell. so when it is clicked it shows data related to the first selection.
I am enabling my user to go back to any of the cells to change their selection.. or maybe they might go back thinking they want to change but don't.
In which case for the second tableviewcell of my parentview will either need to change if the first cell changes or stay the same if the value of the first cell dosnt change.
I have this delegate that is used with the first cell, and it is where I am trying to control the value of the secondcell, as shown below.
- (void) setManufactureSearchFields:(NSArray *)arrayValues withIndexPath:(NSIndexPath *)myIndexPath
{
manufactureSearchObjectString = [[arrayValues valueForKey:#"MANUFACTURER"] objectAtIndex:0];
manufactureIdString = [[arrayValues valueForKey:#"MANUFACTURERID"] objectAtIndex:0]; //Restricts Models dataset
manufactureResultIndexPath = myIndexPath;
[self.tableView reloadData]; //reloads the tabels so you can see the value in the tableViewCell.
//need some sort of if statment here so that if the back button is pressed modelSearchObjectString is not changed..
if (oldManufactureSearchObjectString != manufactureSearchObjectString) {
modelResultIndexPath = NULL;
modelSearchObjectString = #"empty";
oldManufactureSearchObjectString = manufactureSearchObjectString;
}
}
The thing being is that it enters the if statment every time even if the same cell is selected for the first cell.. (in which case is should not enter the if statement.
I thought I could do this by checking oldMan vrs man if != then go through and set the second cell stuff and then pass man to oldMan so next time you use the first cell it has a value to comapre against. but obviously this dosn't seem to be working. Is my logic bad or is it something in my code.
this is how I set these values in .h they are all #synthesised
//...
NSString *manufactureSearchObjectString;
NSString *oldManufactureSearchObjectString;
NSString *manufactureIdString;
NSIndexPath *manufactureResultIndexPath;
//...
#property (copy) NSString *manufactureSearchObjectString;
#property (copy) NSString *oldManufactureSearchObjectString;
#property (copy) NSString *manufactureIdString;
#property (copy) NSIndexPath *manufactureResultIndexPath;
//...
You have to compare the value of the NSStrings as below:
BOOL isEqual = [aString isEqualToString:bString]; // provide aString and bString
if (!isEqual)
{
// code
}
What you've done is to compare the pointers, which can be different for strings of the same value.

UITextField isn't working

am new to objective-c programming, so sorry if it's a silly question but i looked thoroughly and couldn't find the error,
p.s: english isn't my first language so programming keywords might be unintentionally used
I've set up a txtfield and defined some of it's properties, such as the keyboard-number pad and stuff, the problem is, when i run it on the simulator, nothing happens, the defult keyboard is called.
i'm just trying to do simple stuff to get it in my head,
in my .h, i have
#interface practiceViewController : UIViewController <UITextFieldDelegate>
{
UITextField *userInput;
}
#property (nonatomic, retain, readwrite) UITextField *userInput;
#end
and in my .m
-(UITextField *)userInput{
if (userInput == nil) {
userInput=[[UITextField alloc]init];
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
}
return userInput;
}
i also tried to make it as an input, but for some reson it's not working
int nInput=[[userInput text] intValue];
whats pissing me off about this is that i did the same in a previous project and the "input worked" only, but now everything isn't.
and my property as i haven't defined anything.
i appreciate your help
I Think you have to set the frame for UITextField like this ...
userInput.frame=CGRectMake(100,100,60,31);
in your -(UITextField *)userInput method, that's y it didn't show ..
Well few things are hazy but ...
You are getting the value of the text field using,
int nInput = [[userInput text] intValue];
If you are not getting the correct value then nInput will be zero. That is because userInput must be nil. You can verify this by logging it by adding NSLog(#"%#", userInput); before the line above.
Whatever value is printed, it looks like your on screen text field is not referenced to by userInput.
Since you're doing the entire thing programmatically, you will have to add your userInput to the view hierarchy by doing this,
[self.view addSubview:self.userInput];
Using the property accessor is important here is important as otherwise userInput would be nil unless you've accessed it using self.userInput earlier.
Although this is tagged iPhone, do you happen to be simulating an iPad? If so, I have run into this. The iPad will not show a number pad keyboard.
userInput= [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 200, 30)]
userInput.keyboardType= UIKeyboardTypeNumberPad;
userInput.returnKeyType= UIReturnKeyDone;
userInput.textColor=[ UIColor redColor];
userInput.clearButtonMode= UITextFieldViewModeWhileEditing;
userInput.delegate = self;
You can just set the frame and add below property(to visible the textfield frame in View otherwise it is not visible in View).
userInput.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:userInput];
I hope it will be helpful to you.