Sunday, June 30, 2013

mutating method sent to immutable object NSUserDefaults http://stackoverflow.com/questions/5790715/mutating-method-sent-to-immutable-object

http://stackoverflow.com/questions/5790715/mutating-method-sent-to-immutable-object

mutating method sent to immutable object NSUserDefaults


When I use this method first time it works fine, but when I called it second time I get the error "mutating method sent to immutable object". The problem is at line with "addObject" command.

  -(IBAction) save: (id) sender{    NSMutableArray *placesT= [[NSUserDefaults standardUserDefaults] objectForKey:@"placesT"];    if (!placesT) {      placesT=[[[NSMutableArray alloc] init] autorelease];  }    [placesT addObject: [NSString stringWithFormat:@"%@", tagF.text] ];    NSUserDefaults *tUD=[NSUserDefaults standardUserDefaults];  [tUD setObject:placesT forKey:@"placesT"];  [tUD synchronize];    [self dismissModalViewControllerAnimated:YES];

}


That is because the object stored in the NSUserDefaults is not the mutableArray but a normal array.

  -(IBAction) save: (id) sender{       NSMutableArray *placesT= nil;     NSrray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"placesT"];    if (tempArray) {      placeT = [tempArray mutableCopy];  } else {      placesT=[[NSMutableArray alloc] init];  }    [placesT addObject: [NSString stringWithFormat:@"%@", tagF.text] ];    NSUserDefaults *tUD=[NSUserDefaults standardUserDefaults];  [tUD setObject:placesT forKey:@"placesT"];  [tUD synchronize];    [self dismissModalViewControllerAnimated:YES];  [placesT releae];  }
   placesT is a non mutable array, either always set placesT a mutable object always or use following code.
  NSMutableArray *placesT= [[[NSUserDefaults standardUserDefaults] objectForKey:@"placesT"] mutableCopy];

Wednesday, June 19, 2013

Get underlying NSData* from UIImage http://stackoverflow.com/questions/4623931/get-underlying-nsdata-from-uiimage

http://stackoverflow.com/questions/4623931/get-underlying-nsdata-from-uiimage

Get underlying NSData* from UIImage

I can create UIImage from NSData using [UIImage imageWithdata:] or [UIImage initWithData] method.

I wonder if I can get the NSData* back from an existing UIImage* ?
something on the line of NSData* myData = [myImage getData];

Is this impossible?
Thank you

  NSData *imageData = UIImageJPEGRepresentation(image, 0.7); // 0.7 is JPG quality

or

  NSData *imageData = UIImagePNGRepresentation(image);

Depending if you want your data in PNG format or JPG format.

Thursday, June 13, 2013

UISegmentedControl in the Navigation Bar with the Back button http://stackoverflow.com/questions/15383014/uisegmentedcontrol-in-the-navigation-bar-with-the-back-button

http://stackoverflow.com/questions/15383014/uisegmentedcontrol-in-the-navigation-bar-with-the-back-button

UISegmentedControl in the Navigation Bar with the Back button


I'm adding a UISegmentedControl to the Navigation bar programatically where the titleViewshould be. But as Apple docs have mentioned under titleViewThis property is ignored if leftBarButtonItem is not nil.

But I want to have the back button as well. Like they have illustrated in their own images!

enter image description here

Below is the code I add the UISegmentedControl.

  self.navigationItem.leftBarButtonItem = nil;  UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil];  [statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];  self.navigationItem.titleView = statFilter;

Is there another way to add a UISegmentedControl along with the Back button as well?

Thank you.

asked Mar 13 at 10:42
Isuru
1,04811031

You can create a UIBarButtonItem with a custom view which could potentially be yourUISegmentedControl.

Something along the lines of the following may work.

  //create segmented control with items  UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"One", @"Two", nil]];    //create bar button item with segmented control as custom view  UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];    //add segmented control bar button item to navigation bar  [[[self navigationController] navigationItem] setRightBarButtonItem:barButtonItem];

I haven't tested this but it should be along the right lines of what you need.

answered Mar 13 at 10:48
CaptainRedmuff
1,316419
Hi thanks for the response. In the meantime I was waiting, I slapped together a small program to test it out. I put 2 View Controllers, a button in the first one to segue to the other one. And in the ViewDidLoad method of the second View Controller, I created the UISegmentedControl using the code I've posted in my question andvoila! It works! I don't know why Apple has said it won't work. :S – Isuru Mar 13 at 11:10

Try this

Remove this line --- > self.navigationItem.leftBarButtonItem = nil;

Add this instead

  UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil]];  [statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];  [statFilter sizeToFit];  self.navigationItem.titleView = statFilter;

Only change is I have added this line :

  [statFilter sizeToFit];

Hope this Helps !!!