I'm trying to change the alignment of a UILabel on the main thread like this
[updateLabel performSelectorOnMainThread:#selector(setTextAlignment:) withObject:UITextAlignmentCenter waitUntilDone:YES];
However this generates a "makes pointer from integer without cast error. What's the correct way to do this?
This is not going to work because UITextAlignmentCenter is not a pointer to an object, but a simple integer. A solution is to implement an own method on the view controller that sets the alignment to center via updateLabel.textAlignment = UITextAlignmentCenter; and calling [yourViewController performSelectorOnMainThread:#selector(setUpdateLabelTextAlignmentToCenter) withObject:nil waitUntilDone:YES];
You're passing a raw integer to "withObject" instead of an object, that's why you're getting the error. Instead, you could call a selector of your own on the main thread that does the job for you, passing in a dictionary or array containing the UILable object to change, and the new alignment value packaged up as a NSNumber:
- (void) changeLabelAlignment:(NSArray *)argsArray
{
UILabel *lbl = [argsArray objectAtIndex:0];
int alignment = [[argsArray objectAtIndex:1] intValue];
lbl.textAlignment = alignment;
}
calling it this way:
...
NSArray *argsArray = [NSArray arrayWithObjects:label, [NSNumber numberWithInt: UITextAlignmentCenter], nil];
[myClassObject performSelectorOnMainThread:#selector(changeLabelAlignment:)
withObject:argsArray
waitUntilDone:YES];
Related
I have face strange problem on UIButton.
When i tap button the app is crash .
I wrote below code for that...
-(IBAction)renameTest:(id)sender
{
NSLog(#"Tapped");
// UIButton *button = (UIButton *)sender;
NSUInteger row = 1;//button.tag;
NSString * titlename = [titleArray objectAtIndex:row];
RenameTest *renameVC = [[RenameTest alloc]initWithNibName:#"RenameTest" bundle:nil];
renameVC.titlespell = titlename;
NSLog(#"titlespell = %#",renameVC.titlespell);
NSLog(#"title = %#",titlename);
// [button release];
[self.navigationController pushViewController:renameVC animated:YES]; //here APP is cresh
[renameVC release];
}
I check also my .Xib file name .It is ok and files are there.
error msg is below :
2012-07-11 14:28:29.079 TestApp[238:207] -[__NSCFDictionary _isNaturallyRTL]: unrecognized selector sent to instance 0x73d8a80
Thanks in Advance.
[button release] is causing the problem. Remove it and check.
_isNaturallyRTL is an NSString method (private), and it looks like you are passing a dictionary instead of a string somewhere.
Breaking on the exception and showing us the call stack at that point would help tremendously.
If u have created the button in xib file then u cannot release it because you have not allocated it and claimed ownership.. U should call release only on objects you have allocated by calling alloc..
Remove the [button release] statement .. that should fix the crash!
You have a crash which is related to a dictionary and your titlename string is set equal to, titleArray objectAtIndex:row. I believe, without seeing the declaration of your variables, that titleArray is a dictionary, or is a NSMutableArray importing from a plist of dictionaries, either way you need to use objectForKey, when using dictionaries, like this:
[[titleArray objectAtIndex:(NSUInteger *)] objectForKey:(NSString *)]
Obviously replace (NSUInteger *) with your integer row and (NSString *) with the name of your key. This may not be the answer but from your crash report and visible code, this is what I assume.
Instruments is saying there is a memory leak in this code:
- (void)layoutImageMaskViewForImageAtPath:(NSString *)path withFillColor:(UIColor *)color indexPath:(NSIndexPath *)indexPath {
UIImage *image = [UIImage imageWithContentsOfFile:path];
[self layoutImageMaskViewForImage:image withFillColor:color indexPath:indexPath];
}
UIColor *anIconFillColor = [UIColor colorWithWhite:0.70 alpha:1.0];
NSIndexPath *anIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSString *aPlaceholderPath = [[NSBundle mainBundle] pathForResource:#"path" ofType:#"png"];
[self layoutImageMaskViewForImage:anImage withFillColor:anIconFillColor indexPath:anIndexPath];
and
NSDictionary *anAssignedData = [aReservationData objectForKey:kAssignedSectionKey];
NSMutableArray *anEmployeeTaskQueueList = [NSMutableArray array];
NSArray *anAssignedReservationData = [anAssignedData objectForKey:kEmployeesIdentifier];
for (NSDictionary *aJobQueueData in anAssignedReservationData) {
EmployeeReservationQueue *anAssignedTaskQueue = [[EmployeeReservationQueue alloc] initWithServerDictionary:aJobQueueData];
if (anAssignedTaskQueue.rows.count == 0) {
ReservationTrack *aTrack = [[ReservationTrack alloc] init];
aTrack.rowSortOrder = 0;
aTrack.reservations = [NSArray array];
anAssignedTaskQueue.rows = [NSArray arrayWithObject:aTrack];
[aTrack release];
}
[anEmployeeTaskQueueList addObject:anAssignedTaskQueue];
[anAssignedTaskQueue release];
}
Your second example leaks track. Your last line is releasing aTrack instead.
In second case here:
[aTrack release];
What is aTrack? May be you mean [track release];?
In first case probably that you pass to function non-autoreleased parameters or may be you are not releasing them after calling that method. Just post code where you call for that method and I will check.
Gold memory-management rule in Objective-C :
Each 'init', 'copy','mutableCopy','retain' must call then 'release' or 'autorelease'.
Instruments reports that your app is leaking a ReservationTrack object. By default it shows where the leaked object was allocated, which is the code you posted. The code you posted doesn't leak a ReservationTrack. It stores it in an EmployeeReservationQueue which is stored in an NSMutableArray. One possibility is that you later access the ReservationTrack object, send it retain, and don't send it release or autorelease. Another possibility is that you leak the EmployeeReservationQueue or the NSMutableArray.
If you use the simulator, you can see the full retain/release history of most objects. When a leaked object shows up, mouse over the address of the object and click the right arrow that appears next to the address. Instruments will show you every malloc, retain, release, and autorelease event for that object. If you choose View > Extended Detail from the menu bar, you can click on any of those events and see the stack trace of the event. This should help you track down the unbalanced retain.
I'm looking for some help. I've tried searching this site and have tried amending my code but I'm stuck. My issue is with displaying and editing an integer attribute stored in coredata. I have a detailview which uses a UITableView. It seems to display the integer correctly in the cell, using the following code:
cell.textLabel.text = #"Set target";
cell.detailTextLabel.text = [match.set_target stringValue];
but, when I try and edit the value by passing it to a UITextField on an editing view, it displays the integer incorrectly (for example 3 is displayed as 53916). I'm passing the value to the UITextField with this code: (note, editedObject is NSManagedObject, numField is UITextField, editedFieldKey is NSString).
[numField setText:[NSString stringWithFormat:#"%d", editedFieldKey]];
The values are passed to the editing view from the detail view using this code:
controller.editedFieldKey = #"set_target";
controller.editedFieldName = NSLocalizedString(#"Number of sets to win", #"set_legs");
I can display, edit and save strings and dates but I can't figure out Integers. Any help would be appreciated.
EDIT 1
I have save and cancel buttons on my edit view. The save button invokes:
- (IBAction)save {
// Set the action name for the undo operation.
NSUndoManager * undoManager = [[editedObject managedObjectContext] undoManager];
[undoManager setActionName:[NSString stringWithFormat:#"%#", editedFieldName]];
if (editingDate) {
[editedObject setValue:dateField.date forKey:editedFieldKey];
}
else if (editingNum) {
[editedObject setValue: [NSNumber numberWithInteger: [numField.text integerValue]] forKey: editedFieldKey];
}
else {
[editedObject setValue: textField.text forKey:editedFieldKey];
}
[self.navigationController popViewControllerAnimated:YES];
}
With the code for displaying the integer set as:
[numField setText:[NSString stringWithFormat:"%d", [editedObject valueForKey:editedFieldKey]]];
I have a warning about the line: Passing argument 1 of 'StringwithFormat' from incompatible pointer type.
On running it crashes with: +[NSString WithFormat:]: unrecognized selector sent to class 0x211d60'
You are passing a string to numberWithInteger, when instead you want the integer value from the string. Try changing it to this:
[editedObject setValue: [NSNumber numberWithInteger: [numField.text integerValue]] forKey: editedFieldKey]; .
"editedFieldKey is NSString"
[numField setText:[NSString stringWithFormat:#"%d", editedFieldKey]];
replace the formatter with %#
[numField setText:[NSString stringWithFormat:#"%#", editedFieldKey]];
EDIT:
Since you are not adding anything to the string, the stringWithFormat: message is unnecessary.
The following will product the same result.
[numField setText:editedFieldKey];
And for those who prefer '.' syntax for property accessors
numField.text = editedFieldKey;
EDIT 2
I will assume that you are passing in your managed object and context to the editing controller...
To display the value of the "set_target" attribute of your managed object.
[numField setText:[[editedObject valueForKey:editedFieldKey] stringValue]]; // set_target is returned as an NSNumber
I have 4 UITextFields that I'm dynamically creating, in the viewDidLoad, which works good. I want to reference those objects when the UISlider value changes. Right now I'm storing those objects in a NSMutableArray and accessing them like so from the sliderChanged method:
NSInteger labelIndex = [newText intValue];
labelIndex--;
NSUInteger firstValue = (int)0;
NSMutableArray *holeArray = [pointsArray objectAtIndex:labelIndex];
UITextField *textField = [textFieldArray objectAtIndex:firstValue];
NSString *newLabel1Text = [[NSString alloc] initWithString:[[holeArray objectAtIndex:firstValue] stringValue]];
[textField setText: newLabel1Text];
[newLabel1Text release];
Everything is working good, but the program crashes on the setText: method. The last message I get from the program is: [UILabel drawTextInRect:] and then I get a EXC_BAD_ACCESS failure.
I want to be able to acces that dynamically created UITextField, but I must be going about it the wrong way.
Thanks!
Uh, yea, you create a text field, but you aren't displaying the field itself, just creating it.
If you want to do what I think you want to do, I would just do if statements.
ex.
if (firstValue == 1)
{
fieldone.text = #"whatever";
}
else if (firstValue == 2)
{
fieldtwo.text = #"whatever";
}
I'm setting a UILabel to a value stored in as a NSNumber.
If I do this
foo.label.text = [bar stringValue];
then I get a NSInvalidArgumentException
However if I cast by doing:
foo.label.text = (NSString *)bar;
then things work ok.
Can anyone explain why this could be the case?
If bar is an NSString instance, it is normal that you get a NSInvalidArgumentException, because NSString does not respond to stringValue selector; thus the result of the stringValue is an invalid value for the text property.
Using your example, if I do the following:
NSNumber *theNumber = [NSNumber numberWithInt:1];
[[self theLabel] setText:(NSString *)theNumber];
I get the NSInvalidArguementException. But if I do:
NSNumber *theNumber = [NSNumber numberWithInt:1];
[[self theLabel] setText:[theNumber stringValue]];
My label gets set with the number 1.
Are you sure bar is NSNumber? Is foo a view with a label subview defined?
For my example, I have an IBOutlet property on an UIViewController to a UILabel. I put that label in the view using InterfaceBuilder and connected the IBOutlet.