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?
Related
The default scroll setting for a UIPickerView is set to vertical. Is it possible to implement a UIPickerView horizontally?
If so, could you please show me a sample or direct me where to helpful documentation?
you can use CPPickerView .. A custom, configurable, horizontal version of UIPickerView (based on the spinning-wheel or slot-machine metaphor), with an included table cell implementation. Originally intended for condensing the space/rows needed for a multi-option setting.
IN .h File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIPickerViewDelegate> {
IBOutlet UIPickerView *pickerView;
NSMutableArray *itemArray;
IBOutlet UILabel *myLabel;
}
#property (nonatomic, retain) UIPickerView *pickerView;
#property (nonatomic, retain) UILabel *myLabel;
#end
IN .XIB File
drag and drop UIPickerView And One UILable
Also connect BOTH the “delegate” and “Referencing Outlet” to the FileOwner.
IN .M File
#import "ViewController.h"
#implementation ViewController
#synthesize pickerView, myLabel;
- (void)didReceiveMemoryWarning
{
[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, typically from a nib.
self.pickerView.delegate = self;
self.pickerView.showsSelectionIndicator =YES;
self.pickerView.backgroundColor = [UIColor blackColor];
CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI_2);
rotate = CGAffineTransformScale(rotate, 0.1, 0.8);
[self.pickerView setTransform:rotate];
self.pickerView.center = CGPointMake(160,75);
UILabel *theview[20];
CGAffineTransform rotateItem = CGAffineTransformMakeRotation(-M_PI_2);
rotateItem = CGAffineTransformScale(rotateItem, 1, 10);
for (int i=0;i<20;i++) {
theview[i] = [[UILabel alloc] init];
theview[i].text = [NSString stringWithFormat:#"%d",i];
theview[i].textColor = [UIColor blackColor];
theview[i].frame = CGRectMake(0,0, 100, 100);
theview[i].backgroundColor = [UIColor clearColor];
theview[i].textAlignment = NSTextAlignmentCenter; //UITextAlignmentCenter is deprecated.
theview[i].shadowColor = [UIColor whiteColor];
theview[i].shadowOffset = CGSizeMake(-1,-1);
theview[i].adjustsFontSizeToFitWidth = YES;
UIFont *myFont = [UIFont fontWithName:#"Georgia" size:15];
[theview[i] setFont:myFont];
theview[i].transform = rotateItem;
}
itemArray = [[NSMutableArray alloc] init];
for (int j=0;j<20;j++) {
[itemArray addObject:theview[j]];
}
}
#pragma mark -
#pragma mark Picker View Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [itemArray count];
}
- (UIView *)pickerView:(UIPickerView *)thePickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
return [itemArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
myLabel.text = [NSString stringWithFormat:#"SELECTED: %d", row+1];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (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
I'm attempting to implement a pop-up UIPickerView to emulate the behavior when clicking on drop-down field within Safari with limited success so far. I found:
Bottom pop-up UIPicker?
and have attempted to follow this. So far, I have my PopUpPickerView showing, but the UIPickerView itself does not (I just see the view background color showing).
PopUpPickerViewController has a nib file so that when a button is clicked, the 'change' selector is invoked and the UIPickerView should pop up. At the moment, only the background color of the PopUpPickerView pops up.
Any help you could provide would be very much appreciated. Thanks in advance!
My code is as follows:
PopUpPickerViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PopUpPickerView.h"
#define UIColorMakeRGBA(nRed, nGreen, nBlue, nAlpha) [UIColor colorWithRed: (nRed)/255.0f green: (nGreen)/255.0f blue:(nBlue)/255.0f alpha:nAlpha]
#class PopUpPickerView;
#interface PopUpPickerViewController : UIViewController{
NSArray *pickerData;
PopUpPickerView *pickerView;
UIPickerView *picker;
}
-(IBAction)change:(id)sender;
-(void)addSubviewToWindow:(UIView*) addView;
#property(nonatomic, retain) PopUpPickerView *pickerView;
#property(nonatomic, retain) NSArray *pickerData;
#property(nonatomic, retain) UIPickerView *picker;
#end
PopUpPickerViewController.m
#import "PopUpPickerViewController.h"
#import "PopUpPickerView.h"
#implementation PopUpPickerViewController
#synthesize pickerData, pickerView, picker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.pickerView = [[PopUpPickerView alloc] initWithFrame:CGRectMake(0.0, 174.0, 320.0, 286.0)];
self.picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 70.0, 320.0, 216.0)];
self.pickerView.picker = self.picker;
//[self.picker addTarget:self action:#selector(pickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.pickerView.parentViewController = self;
[self.pickerView addSubview:self.picker];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)change:(id)sender{
PopUpPickerView *pView = (PopUpPickerView*) pickerView;
[pView animateDatePicker:YES];
}
-(void)addSubviewToWindow:(UIView*) addView{
[self.view addSubview:addView];
}
#end
PopUpPickerView .h
#import <UIKit/UIKit.h>
#import "PopUpPickerViewController.h"
#class PopUpPickerViewController;
#interface PopUpPickerView : UIView<UIPickerViewDelegate, UIPickerViewDataSource>{
UIPickerView *picker;
PopUpPickerViewController *parentViewController;
NSArray *pickerData;
}
-(void)animateDatePicker:(BOOL)show;
#property(nonatomic, retain) UIPickerView *picker;
#property(nonatomic, retain) NSArray *pickerData;
#property(nonatomic, retain) PopUpPickerViewController *parentViewController;
#end
PopUpPickerView.m
#import "PopUpPickerView.h"
#import "PopUpPickerViewController.h"
#implementation PopUpPickerView
#synthesize picker, pickerData, parentViewController;
- (void)animateDatePicker:(BOOL)show {
pickerData = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
self.picker.delegate = self;
self.picker.dataSource = self;
CGRect screenRect = self.frame;
CGSize pickerSize = [self.picker sizeThatFits:CGSizeZero];
CGRect startRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width, pickerSize.height);
CGRect pickerRect = CGRectMake(0.0,
screenRect.origin.y + screenRect.size.height,
pickerSize.width,
pickerSize.height);
self.picker.frame = pickerRect;
self.backgroundColor = UIColorMakeRGBA( 255, 125, 64, 0.7f - (int)show * 0.7f );
if ( show ) {
self.picker.frame = startRect;
PopUpPickerViewController *controller = (PopUpPickerViewController*) self.parentViewController;
[controller addSubviewToWindow:self];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationDelegate:self];
self.backgroundColor = UIColorMakeRGBA( 255, 125, 64, 0.0f + (int)show * 0.7f );
if ( show ) {
self.picker.frame = pickerRect;
} else {
[UIView setAnimationDidStopSelector:#selector(slideDownDidStop)];
self.picker.frame = startRect;
}
[UIView commitAnimations];
}
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.pickerData objectAtIndex:row];
}
#end
It turned out that the answer was to do with the coordinates at which the UIPickerView was placed. The UIPickerView was being placed at the bottom of the PopUpPickerView which meant that it was offscreen. After adjusting the coordinates, it all worked out fine.
I have used SBTableAlert several times and it seems to provide what you require but with a UITableView instead of a UIPickerView. Take a look and hope it helps in your project.
Git Repo
Here is my .h file
When I run this code and select a text field, a pickerview pop's with question marks in it
but when I select question mark I get correct values in text field
#import <UIKit/UIKit.h>
#import "ScrollableViewController.h"
#import "MIBackgroundTapDelegate.h"
//#interface Activapc1 : UIViewController {
#interface Activapc1 : ScrollableViewController <MIBackgroundTapDelegate, UIPickerViewDelegate, UIPickerViewDataSource>{
UITextField *amplitude1;
UITextField *rate1;
UITextField *pulse_width1;
UITextField *impedance1;
IBOutlet UITextField *configuration;
NSArray *mode;
}
#property (nonatomic, retain) IBOutlet UITextField *amplitude1;
#property (nonatomic, retain) IBOutlet UITextField *rate1;
#property (nonatomic, retain) IBOutlet UITextField *pulse_width1;
#property (nonatomic, retain) IBOutlet UITextField *impedance1;
-(IBAction)next;
-(IBAction)skip;
-(IBAction)home;
-(IBAction)select;
-(IBAction)textFieldDoneEditing:(id) sender;
#end
Here is .m file
I am not able to understand why I am getting question mark please help me
#import "Activapc1.h"
#import "dbsViewController.h"
#import "Activapc2.h"
#import "APCoption.h"
#implementation Activapc1
#synthesize amplitude1;
#synthesize rate1;
#synthesize pulse_width1;
#synthesize impedance1;
const int MyModePicker = 3002;
-(void)viewDidLoad {
self.svScrollViewM.contentSize = CGSizeMake(320, 416);
[self registerForEditingEvents:amplitude1];
[self registerForEditingEvents:rate1];
[self registerForEditingEvents:pulse_width1];
[self registerForEditingEvents:impedance1];
UIPickerView *modePicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
modePicker.tag = MyModePicker;
modePicker.delegate = self;
modePicker.dataSource = self;
[modePicker setShowsSelectionIndicator:YES];
configuration.inputView = modePicker;
[modePicker release];
[super viewDidLoad];
//Prepare data for pickers
mode = [NSArray arrayWithObjects:#"Voltage",#"Current",nil];
[mode retain];
//mode = [[NSMutableArray alloc] init];
//[mode addObject]
}
//********picker view code**************
#pragma mark -
#pragma mark UIPickerViewDelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titileForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if(pickerView.tag == MyModePicker)
{
return [mode objectAtIndex:row];
}
return #"Unknown title";
}
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView.tag == MyModePicker)
{
configuration.text = (NSString *)[mode objectAtIndex:row];
}
}
#pragma mark -
#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(pickerView.tag == MyModePicker)
{
return mode.count;
}
return 1;
}
#pragma mark -
//**********picker view code end************
-(IBAction) backgroundTap:(id) sender{
[self.amplitude1 resignFirstResponder];
[self.rate1 resignFirstResponder];
[self.pulse_width1 resignFirstResponder];
[self.impedance1 resignFirstResponder];
}
-(IBAction) textFieldDoneEditing:(id)sender{
[sender resignFirstResponder];
}
-(IBAction)home {
dbsViewController *dbs = [[dbsViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:dbs animated:YES];
}
-(IBAction)skip {
Activapc2 *activapc2 = [[Activapc2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:activapc2 animated:YES];
}
-(IBAction)next {
Activapc2 *activapc2 = [[Activapc2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:activapc2 animated:YES];
}
-(IBAction)select {
APCoption *apcoption = [[APCoption alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:apcoption 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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[mode release];
[super dealloc];
}
#end
The pickerview needs a title not a titile. Also the delegate method that returns the string does not have a reusingView: parameter. Since you don't properly implement the delegate method the picker simply displays question marks.
-(NSString *)pickerView:(UIPickerView *)pickerView titileForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
I believe it should be:
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
If I run Build and Analyze this loads without errors according to the Analyzer but the app crashes. If I remove the [myStates release]; analyzer complains about possible leak but the nib loads and runs just fine. MyStateList is a nib which has a pickerview inside that loads a plist if this helps. Please help.
Main TrialViewControllerViewController Implementation File
#import "TrialViewControllerViewController.h"
#import "MyStateList.h"
#implementation TrialViewControllerViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyStateList *myStates = [[MyStateList alloc] initWithNibName:#"MyStateList" bundle:nil];
[self.view addSubview:[myStates view]];
//[myStates release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
Here is what i am trying to load
MyStateList.h
#import <UIKit/UIKit.h>
#interface MyStateList : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
UIPickerView *pickerView;
NSMutableArray *statesArray; // Written array
NSDictionary *stateListDictionary,*stateListDictionaries;
NSArray *firstDisplayArray,*updateDisplayArray,*switchDisplayArray;
NSInteger dTag;
NSString *nott,*verify,*notes;
IBOutlet UILabel *labelOne,*labelTwo,*labelThree,*labelName;
}
#property (nonatomic, retain) UIPickerView *pickerView;
#property (nonatomic, retain) NSDictionary *stateListDictionary,*stateListDictionaries;
#property (nonatomic, retain) NSArray *firstDisplayArray,*updateDisplayArray,*switchDisplayArray;
#property (nonatomic, retain) IBOutlet UILabel *labelOne,*labelTwo,*labelThree,*labelName;
#property (nonatomic, retain) NSString *nott,*verify,*notes;
- (void)loadData;
- (void)placeData;
- (void)createPicker;
#end
MyStateList.m
#import "MyStateList.h"
#implementation MyStateList
#synthesize pickerView;
#synthesize stateListDictionary,stateListDictionaries,firstDisplayArray,updateDisplayArray,switchDisplayArray ;
#synthesize labelOne,labelTwo,labelThree,labelName;
#synthesize nott,verify,notes;
- (void)viewDidLoad
{
[super viewDidLoad];
[self createPicker];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [statesArray count];
}
-(UIView *)pickerView:(UIPickerView *)pickerViewCust viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view
{
NSString *rowItem = [statesArray objectAtIndex: row];
UILabel *lblRow = [[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerViewCust bounds].size.width, 44.0f)]autorelease];
[lblRow setTextAlignment:UITextAlignmentCenter];
[lblRow setTextColor: [UIColor blueColor]];
[lblRow setText:rowItem];
[lblRow setBackgroundColor:[UIColor clearColor]];
return lblRow;
}
- (void)createPicker
{
NSString *path = [[NSBundle mainBundle] pathForResource:
#"StateArray" ofType:#"plist"];
stateListDictionary = [NSDictionary dictionaryWithContentsOfFile:path];
labelName.text = [NSString stringWithFormat:#"Arizona"];
[self loadData];
float screenWidth = [UIScreen mainScreen].bounds.size.width;
float pickerWidth = screenWidth * 1 / 2;
float xPoint = screenWidth / 2 - pickerWidth / 1;
pickerView = [[UIPickerView alloc] init];
[pickerView setDataSource: self];
[pickerView setDelegate: self];
[pickerView setFrame: CGRectMake(xPoint, 280.0f, pickerWidth, 180.0f)];
pickerView.showsSelectionIndicator = YES;
[pickerView selectRow:2 inComponent:0 animated:YES];
[self.view addSubview: pickerView];
}
- (void)loadData
{
firstDisplayArray = [stateListDictionary objectForKey:#"Arizona"];
dTag = 1;
[self placeData];
stateListDictionary = nil; // kill the list
statesArray = [[NSMutableArray alloc] init];
[statesArray addObject:#"Alabama"];
[statesArray addObject:#"Alaska"];
[statesArray addObject:#"Arizona"];
[statesArray addObject:#"Arkansas"];
[statesArray addObject:#"California"];
}
- (void)placeData
{
if (dTag == 1)
{
switchDisplayArray = firstDisplayArray;
dTag = 0;
} else {
switchDisplayArray = updateDisplayArray;
}
nott = [switchDisplayArray objectAtIndex:0];
verify = [switchDisplayArray objectAtIndex:1];
notes = [switchDisplayArray objectAtIndex:2];
labelOne.text = nott;
labelTwo.text = verify;
labelThree.text = notes;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
NSString *path = [[NSBundle mainBundle] pathForResource:
#"StateArray" ofType:#"plist"];
stateListDictionaries = [NSDictionary dictionaryWithContentsOfFile:path];
labelName.text = [statesArray objectAtIndex: row];
updateDisplayArray = [stateListDictionaries objectForKey:labelName.text];
[self placeData];
}
- (void)dealloc
{
[pickerView release];
[stateListDictionary release];
[stateListDictionaries release];
[statesArray release];
[super dealloc];
}
#end
If I had to guess, MyStatesList is a subclass of UIViewController.
If you release myStates at the end of the block, you're essentially removing the brain from your view controller, but leaving its body there. You need to keep the controller itself around as well as the view, as the view is owned by the controller. Not the other way around.
The view from your view controller is retained by the view, but you killed the view controller itself. The bigger problem is you CAN'T just add views from UIViewControllers in this manner. On iOS5 you have the ability to use addChildViewController: and prior to that you can use one of the provided container controllers.
You do indeed need to release your MyStateList object.
Here's what's happening:
You initialize a new MyStateList, which has a retain count of 1
You add a subview to your view, and that subview is NOT the MyStateList object, but the ivar view of your MyStateList object.
When you don't release your myStates object, you are indeed leaking this object, since nothing has a reference to this object anymore, and the memory was never deallocated. (Its retain count never reached 0.)
That having been said, what kind of error occurs when your application crashes? I'm guessing that MyStateList is a UIViewController, and the view is then attempting to access/communicate with its parent, which has been released/deallocated.
In your -(void)dealloc; you are calling [super dealloc] first, this is wrong. Make it the last call in the method. This should fix your crash.
- (void)dealloc
{
// Wrong Way
[super dealloc];
[pickerView release];
[stateListDictionary release];
[stateListDictionaries release];
[statesArray release];
}
`
- (void)dealloc
{
// Right way
[pickerView release];
[stateListDictionary release];
[stateListDictionaries release];
[statesArray release];
[super dealloc];
}
you add the view [myStates view], to the controller's view, so the view is hold by the controller's view.
then you release "myStates" for avoid memeory leak, it is removed.
and I guess in your view [myStates view], there must be some delegates related to the object "myState", but it was removed just before. So, the app crash.
To avoid both problems, memory leak and crash. I think you should make the "myStates" hold by your controller as a member variable. And you could release "myState" when your viewUnloaded.
I am using code from Ed Marty's answer for the question here but am having real trouble with a few bits.
On the click of a button I have got the datepicker appearing, but the 'done' button however isn't. I am also getting an error from the line:
[delegate datePickerController:controller didPickDate:datePicker.date];
Error message:
'controller' undeclared (first use in this function)
All in all I have 6 files:
ModalDatePickerViewController.m
ModalDatePickerViewController.h
ModalDatePickerAppDelegate.m
ModalDatePickerAppDelegate.h
DatePickerController.m
DatePickerController.h
My DatePickerController.h is looking like:
#import <UIKit/UIKit.h>
#class DatePickerController;
#protocol DatePickerControllerDelegate
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date;
#end
#interface DatePickerController : UIViewController {
UIDatePicker *datePicker;
NSObject <DatePickerControllerDelegate> *delegate;
}
#property (nonatomic, retain) UIDatePicker *datePicker;
#property (nonatomic, assign) NSObject <DatePickerControllerDelegate> *delegate;
#end
and the DatePickerController.m:
#import "DatePickerController.h"
#implementation DatePickerController
#synthesize datePicker;
#synthesize delegate;
- (void) loadView {
self.view = [[[UIView alloc] init] autorelease];
self.datePicker = [[[UIDatePicker alloc] init] autorelease];
[self.view addSubview:self.datePicker];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.center = CGPointMake(160,230);
[button addTarget:self action:#selector(done) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
- (void) done {
[delegate datePickerController:controller didPickDate:datePicker.date];
}
- (void) dealloc {
[datePicker release];
[super dealloc];
}
#end
On the main view I have a button that call this class as below:
#import "ModalDatePickerViewController.h"
#implementation ModalDatePickerViewController
- (void) pickDate {
DatePickerController *screen = [[[DatePickerController alloc] init] autorelease];
screen.delegate = self;
[self presentModalViewController:screen animated:YES];
}
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date {
//[self doSomethingWithDate:date];
[controller dismissModalViewControllerAnimated:YES];
}
- (IBAction)HitMe:(id)sender {
[self pickDate];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
[super dealloc];
}
#end
and:
#import <UIKit/UIKit.h>
#import "DatePickerController.h"
#interface ModalDatePickerViewController : UIViewController <DatePickerControllerDelegate> {
}
- (IBAction)HitMe:(id)sender;
#end
On this line:
[delegate datePickerController:controller didPickDate:datePicker.date];
try replacing controller with self.