Making UIPicker pop up when textfield is tapped - iphone

I made a application with the templet of a Single View Application. Then I added a label and connected it to the .h file of my ViewController. Then I made a picker, filled it, then set it (and a toolBar I made) to the textfield. But when I tap the textfield, the picker is just all black. If this made no sense, the code will explain it all.
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
}
#property (weak, nonatomic) IBOutlet UITextField *habitField;
#property (weak, nonatomic) NSArray *PickerData;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:#"1",#"2",#"3", nil];
self.PickerData = array;
UIToolbar *toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlackOpaque;
[toolBar sizeToFit];
[toolBar setBackgroundImage:[UIImage imageNamed:#"red_navigation_bar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(releasePicker)];
UIPickerView *Picker = [[UIPickerView alloc] init];
doneButton.image = [UIImage imageNamed:#"button.png"];
[toolBar setItems:#[flexSpace, doneButton] animated:YES];
self.habitField.inputAccessoryView = toolBar;
[self.habitField setInputView:Picker];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.PickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.PickerData objectAtIndex:row];
}
#end
The simulator looks like this

As i see you forgot to set delegate for picker ( UIPickerViewDelegate )
Picker.delegate = self;
Remember to add :)
#interface ViewController : UIViewController<UIPickerViewDelegate> {
}
Cheers

Related

Posting to Facebook from Sharekit

I am newbie to sharekit. I am trying to post a simple text: #"Hello" to Facebook, but facing problems in doing so.. It leads me to the login page. I login and then it closes and comes back to the main screen. But when i check my timeline, it is not added..
My ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
UIButton *btn;
IBOutlet UIToolbar *toolbar;
}
#property (nonatomic,strong) UIButton *btn;
- (IBAction)btnShareClicked:(id)sender;
#end
My ViewController.m
#import "ViewController.h"
#import "SHK.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize btn;
- (void)viewDidLoad
{
[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.
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
self.toolbarItems = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(btnShareClicked:)] ,
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
nil
];
}
return self;
}
- (IBAction)btnShareClicked:(id)sender {
NSString *text = #"Hello";
SHKItem *item = [SHKItem text:text];
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// For Toolbar
//[actionSheet showFromToolbar:toolbar];
[actionSheet showInView:self.view];
}
#end
I have included the MessageUI,Security,SystemConfiguration frameworks... Need some guidance on this..
First You should try to Check whether you are using the updated ShareKit API in Your Application.if Not then Add That Updated One from The SHareKIt Sites.
Here is Link for The ShareKit Site
Here Is the Code See,Called WhenEVer You want to share text Over the Facebook.
-(void)shareOnFacebook{
SHKItem * ietm1= [[SHKItem alloc] init];
[ietm1 setText:#"Hello"];
SHKFacebook *shkFacebook = [[SHKFacebook alloc] init]
[shkFacebook setSHKItem:item];
}
//see whether you have these Classes in your `ShareKit`.
I hope it may Helpful to you.

Navigation Controller in a Tab Controller not showing back button

I have a tab controller with 2 tabs "My information" and "their Information", which is just a bunch of text boxes. I want a Navigation controller at the top so I can go back, but the back button won't show up... here is my code:
Button pressed to show Form:
-(IBAction)onIncidentAidFormPressed:(id)sender {
FormController *aidForm = [[FormController alloc] initWithNibName:#"formController" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:aidForm animated:YES];
}
In FormController.m - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Aid Form", #"Aid Form title");
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithNibName:#"navController" bundle:nil];
[navController pushViewController:viewController1 animated:YES];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return self;
The tabs show up correctly, and the navigation bar is there but no back button is shown to get back.
Edit: Here are the files: IncidentAidStartViewController.h
#import <UIKit/UIKit.h>
#interface IncidentAidStartViewController : UIViewController {
IBOutlet UIButton *incidentAidForm;
IBOutlet UIButton *emergencyContacts;
}
-(IBAction)onIncidentAidFormPressed:(id)sender;
#property (strong, nonatomic) IncidentAidStartViewController *detailViewController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
IncidentAidStartViewController.m
#import "IncidentAidStartViewController.h"
#import "IncidentAidForm.h"
#implementation IncidentAidStartViewController
#synthesize detailViewController = _detailViewController;
#synthesize managedObjectContext = __managedObjectContext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Incident Aid", #"Incident Aid title");
}
return self;
}
#pragma mark - Button handlers
-(IBAction)onIncidentAidFormPressed:(id)sender {
IncidentAidForm *aidForm = [[IncidentAidForm alloc] initWithNibName:#"IncidentAidForm" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:nil];
[self.navigationController pushViewController:aidForm animated:YES];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
IncidentAidForm.h
#import <UIKit/UIKit.h>
#interface IncidentAidForm : UIViewController <UITabBarControllerDelegate> {
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#property (strong, nonatomic) IncidentAidForm *detailViewController;
#property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#end
IncidentAidForm.m
#import "IncidentAidForm.h"
#import "MyInformationController.h"
#import "TheirInformationController.h"
#implementation IncidentAidForm
#synthesize detailViewController = _detailViewController;
#synthesize managedObjectContext = __managedObjectContext;
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Incident Aid Form", #"Incident Aid Form title");
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
MyInformationController.h
#import <UIKit/UIKit.h>
#interface MyInformationController : UIViewController {
IBOutlet UIScrollView *scrollView;
IBOutlet UITextField *firstName;
IBOutlet UITextField *lastName;
IBOutlet UITextField *phoneNumber;
IBOutlet UITextField *address;
IBOutlet UITextField *carMake;
IBOutlet UITextField *carModel;
IBOutlet UITextField *carYear;
IBOutlet UITextField *licensePlate;
IBOutlet UITextField *insuranceCarrier;
IBOutlet UITextField *insurancePolicy;
IBOutlet UIButton *backgroundButton;
}
#property(nonatomic, retain) UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UITextField *firstName;
#property (nonatomic, retain) IBOutlet UITextField *lastName;
#property (nonatomic, retain) IBOutlet UITextField *phoneNumber;
#property (nonatomic, retain) IBOutlet UITextField *address;
#property (nonatomic, retain) IBOutlet UITextField *carMake;
#property (nonatomic, retain) IBOutlet UITextField *carModel;
#property (nonatomic, retain) IBOutlet UITextField *carYear;
#property (nonatomic, retain) IBOutlet UITextField *licensePlate;
#property (nonatomic, retain) IBOutlet UITextField *insuranceCarrier;
#property (nonatomic, retain) IBOutlet UITextField *insurancePolicy;
- (void) animateTextField: (UITextField*) textField up: (BOOL) up;
-(IBAction)hideKeyboard;
#end
MyInformationController.m
#import "MyInformationController.h"
#implementation MyInformationController
#synthesize scrollView, firstName, lastName, phoneNumber, address, carMake, carModel, carYear, licensePlate,
insuranceCarrier, insurancePolicy;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"My Information", #"My Information");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
// Do any additional setup after loading the view, typically from a nib.
firstName.delegate = self;
lastName.delegate = self;
phoneNumber.delegate = self;
address.delegate = self;
carMake.delegate = self;
carModel.delegate = self;
carYear.delegate = self;
licensePlate.delegate = self;
insuranceCarrier.delegate = self;
insurancePolicy.delegate = self;
[scrollView setContentSize:self.view.frame.size];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//[self animateTextField: textField up: YES];
scrollView.frame = CGRectMake(0,44,320,200); //44:NavigationBar ; 200: Keyoard
[scrollView scrollRectToVisible:textField.frame animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//[self animateTextField: textField up: NO];
if (textField.tag == 10) {
scrollView.frame = CGRectMake(0,44,320,416); //original setup
// [textField resignFirstResponder];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
switch (textField.tag) {
case 1:
[lastName becomeFirstResponder];
break;
case 2:
[phoneNumber becomeFirstResponder];
break;
case 3:
[address becomeFirstResponder];
break;
case 4:
[carMake becomeFirstResponder];
break;
case 5:
[carModel becomeFirstResponder];
break;
case 6:
[carYear becomeFirstResponder];
break;
case 7:
[licensePlate becomeFirstResponder];
break;
case 8:
[insuranceCarrier becomeFirstResponder];
break;
case 9:
[insurancePolicy becomeFirstResponder];
break;
case 10:
[textField resignFirstResponder];
break;
default:
break;
}
return TRUE;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)hideKeyboard {
[self.firstName resignFirstResponder];
[self.lastName resignFirstResponder];
[self.phoneNumber resignFirstResponder];
[self.address resignFirstResponder];
[self.carMake resignFirstResponder];
[self.carModel resignFirstResponder];
[self.carYear resignFirstResponder];
[self.licensePlate resignFirstResponder];
[self.insurancePolicy resignFirstResponder];
[self.insuranceCarrier resignFirstResponder];
scrollView.frame = CGRectMake(0,44,320,416); //original setup
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
So far I know this is a good way to add ur views to tabbar. So try this
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
To add back bar item , below ine is also used.
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;
Please modify your method like this
-(IBAction)onIncidentAidFormPressed:(id)sender
{
FormController *aidForm = [[FormController alloc] initWithNibName:#"formController" bundle:nil];
aidForm.managedObjectContext = self.managedObjectContext;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:nil];
[self.navigationController pushViewController:aidForm animated:YES];
}
you should replace your code with the following:
UIViewController *viewController1 = [[MyInformationController alloc] initWithNibName:#"MyInformationController" bundle:nil];
UIViewController *viewController2 = [[TheirInformationController alloc] initWithNibName:#"TheirInformationController" bundle:nil];
UINavigationController *navController1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
UINavigationController *navController2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2, nil];
let me know any thing else you need........

Presenting Modalviewcontroller for Info Button when pressed on UIToolbar programmatically

I want is on main window to present the created modalViewController view when the infobutton is pressed. But when I press Info button on the main window nothing happens.
In the mainviewcontroller.h file I have following code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface imageviewViewController : UIViewController{
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) UIToolbar *toolbar;
#property (nonatomic, assign) UIBarButtonSystemItem currentSystemItem;
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
#end
In the mainviewcontroller.m file have following code:
#import "imageviewViewController.h"
#import "Infoviewcontroller.h"
#implementation imageviewViewController
#synthesize toolbar;
#synthesize currentSystemItem;
#synthesize audioPlayer;
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
}
- (void) Infobuttonpressed: (id) sender
{
Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
[self presentModalViewController:myView animated:YES]; // present view modally
[self.navigationController pushViewController:myView animated:YES]; // add to navigation stack
[myView release];
}
In the Infoviewcontroller.h file have following code:
#import <UIKit/UIKit.h>
#interface Infoviewcontroller : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
#property (nonatomic, retain) UITextView *textView;
#property (nonatomic, assign) UINavigationBar *navBar;
#end
Then in the infoviewcontroller.m file have the following code:
#import "Infoviewcontroller.h"
#implementation Infoviewcontroller
#synthesize textView;
#synthesize navBar;
-(void)dealloc
{
[textView release];
[navBar release];
[super dealloc];
}
-(void)setupTextView
{
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor redColor];
self.textView.font = [UIFont fontWithName:#"System Bold" size:13];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView\nThis is UITextView\nThis is UITextView\nThis is UITextView";
[self.view addSubview: self.textView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
navBar = [[UINavigationBar alloc] init];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:#"ModalViewControllerTest"] autorelease];
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
navItem.rightBarButtonItem = done;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
Should be
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(Infobuttonpressed:)];
Spot the difference? First, you're missing a target (should be self, not nil). Second, the colon at the end of the selector is part of the signature, you were missing it so it was not calling your method (which is InfoButtonPressed:(id)sender).
As an aside, methods should start with a lower case name.
you dont need both pushModalViewController and pushViewController
Assuming your views are already controlled within an instance of a UINavigationController pushModalViewController on its own will work.
edit - remove the reference to pushViewController. Does it now work?
-(void) Infobuttonpressed: (id) sender;
{ Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];
UIViewController *pushController = [[UIViewController alloc] init];
[self addSubview:pushController.view];
[pushController presentModalViewController:navController animated:YES];
[myView release];
}
The target of your infoItem is set to nil. You should set this to an instance of the class where the Infobuttonpressed is defined. I assume that is the mainviewController?
Also, the selector to call is specified as: #selector(Infobuttonpressed), while the method is called "Infobuttonpressed:", that is, with a parameter. That will likely result in a runtime error once you fix the target.
Then, there are some peculiarities in the way you handle things. As Nik already answered, you need to choose whether you want to present the view modally (with presentModalViewController), or present it as part of the navigation stack (with pushViewController), you cannot do both.
If you present it as part of the navitation stack, then it already has a navigation bar, and so you should not add one yourself. Just set the properties of the self.navigationItem.
Have a look at the View Controller Programming Guide for more info.

Bad Exception on dealloc?

I having a problem with a Bad Exception that I could not locate at first, but now have it pinned down on a [super dealloc];, but I have no idea why this happens.
Here is my code :
EditingViewController.h
#interface EditingViewController : UIViewController
{
NSManagedObject *editedObject;
NSString *editedFieldKey;
NSString *editedFieldName;
}
#property (nonatomic, retain) NSManagedObject *editedObject;
#property (nonatomic, retain) NSString *editedFieldKey;
#property (nonatomic, retain) NSString *editedFieldName;
- (IBAction)cancel;
- (IBAction)save;
#end
EditingViewController.m
#import "EditingViewController.h"
#implementation EditingViewController
#synthesize editedObject, editedFieldKey, editedFieldName;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
self.title = NSLocalizedString(editedFieldName, nil);
// Configure the save and cancel buttons.
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (IBAction)save
{
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)cancel
{
// Don't pass current value to the edited object, just pop.
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc
{
[editedObject release];
[editedFieldKey release];
[editedFieldName release];
//[super dealloc];
}
#end
As you can see, I commented the [super dealloc];, which is causing the Bad Exception, but this is obviously not a good solution.
Any idea what I am doing wrong ?
Thanks
The properties might have never been used, so they haven't been initialized in any way
- (void)dealloc
{
self.editedObject = nil;
self.editedFieldKey = nil;
self.editedFieldName = nil;
[super dealloc];
}

UIPickerview AppDelegate problem with subviews

Issue with subviews but it may be related to the AppDelegate with respect to placing a UIPickerView in myView as apposed to self.view?
Thanks
[self.view addSubview:pickerView]; <-- works ok
//[myView addSubview:pickerView];<-- fails, can't get dial movement or response
AppDelegate.h
#import <UIKit/UIKit.h>
#class PickerViewController;
#interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
PickerViewController *pvController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
AppDelegate.m
#import "PickerViewAppDelegate.h"
#import "PickerViewController.h"
#implementation PickerViewAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
pvController = [[PickerViewController alloc] initWithNibName:#"PickerView" bundle:[NSBundle mainBundle]];
[window addSubview:pvController.view];
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)dealloc {
[pvController release];
[window release];
[super dealloc];
}
#end
Controller.h
#import <UIKit/UIKit.h>
#interface PickerViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *arrayColors;
}
#end
Controller.m
#import "PickerViewController.h"
#implementation PickerViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[myView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:myView];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES;
arrayColors = [[NSMutableArray alloc] init];
for (int i=0; i<60; i++) {
[arrayColors addObject:[NSString stringWithFormat:#"%d", i]];
}
//[self.view addSubview:pickerView]; // <-- works
[myView addSubview:pickerView]; // <-- this fails.. this is what I need for multiple views
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[arrayColors release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [arrayColors count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [arrayColors objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(#"Selected Color: %#. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}
#end
If you comment
[myView addSubview:pickerView];
you will understand, that myView frame is too small. You will be able to interact with the picker only touching that parent view frame. If you want to make the pickers behave normally, you must set the parent view frame to contain the picker frame - make it equal or larger.
Please remember iOs Human Interface Guidelines,
The overall size of a picker, including its background, is fixed at the same size as the keyboard on iPhone.
Make sure that myView has userInteractionEnabled set to YES?