Disable delegate methods depending on iOS version - iphone

I'm using some UIPickerView delegate methods only for the brand-new-Ive-modified iOS under NDA, e.g.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
I don't want to implement them for iOS6, to preserve default iOS behavior, so a condition inside the method won't work. How to do it?

In your delegate, override respondsToSelector: and return NO in case of old OS:
- (BOOL)respondsToSelector:(SEL)selector
{
if (selector == #selector(pickerView:viewForRow:forComponent:reusingView:))
return MyFunctionThatChecksIosVersionIsMinimum7();
return [super respondsToSelector:selector];
}
So you're implementing the delegate method normally. When the picker view is asking your delegate if it understands the message you're simply lying to it.

Related

Disable delegate method for UIPickerView/UITableView on conditional case

I want to disable below delegate method for some condition
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
let say if(x==YES) then only above delegate method hit else it won't respond.
Please let me know your suggestion.
Override respondsToSelector: and return NO in case of You does not want to call this method:
- (BOOL)respondsToSelector:(SEL)selector
{
if (selector == #selector(pickerView:viewForRow:forComponent:reusingView:))
{
if(your_Condition)
{
return YES;
}
eles
{
return NO;
}
}
return [super respondsToSelector:selector];
}
So you're implementing the delegate method as normally. When the picker view is asking your delegate if it understands the message you're simply lying to it.
There are two ways you can do it.
1) Make Delegate Null
Set a condition when you do not want to call the method and at that place make delegate = nill.
And again Reset it delegate =self when needs to call the method.
The second method which I must say Better one
2) Use flag inside the Delegate method
Try to implement if-condition (Flag) inside the method and set its TRUE and FALSE values as per requirement.
for ex.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if(x == YES) {
// Only this code need to implement
} else {
return;
}
}

iphone uipickerview with images

it's possible add as item in the uipickerview some images/icons instead of text?
thanks
yes it is absolutely possible. u need to handle pickerView delegate method
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
here you simply return custom view(could be anything UIImage UILabel)
and set userInteractionEnable property to no for customize view..

UIPicker With View + View Tapped != Selected Row

Hey! I have a UIPicker, and I am using the function:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
But, when you tap one of the views returned by this function, the tapped view does not become highlighed, and selected if the touch is released. How can I fix that?
Make use of this property,
pickerView.showsSelectionIndicator = YES;
at initialization.

Very simple iPhone question. How to make UIPickerView with two columns?

I have read other questions about creating a UIPickerView with two or more columns but cannot find the exact solution.
How is this done for the iPhone programmatically? How do you add static data? Thanks!
Make your controller (or whatever is controlling the behavior of the PickerView) support the UIPickerViewDelegate protocol. Then, implement:
- (int) numberOfColumnsInPickerView:(UIPickerView*)picker
to return the number of columns you want, and
- (int) pickerView:(UIPickerView*)picker numberOfRowsInColumn:(int)col
to return the number of rows for each column, and finally:
- (UIPickerTableCell*) pickerView:(UIPickerView*)picker tableCellForRow:(int)row inColumn:(int)col
to setup each cell.
See the reference for UIPickerView and UIPickerViewDelegate.
There is an excellent tutorial on this subject here.
Assuming you have a dictionary or a couple of arrays holding your static data. For simplicity I will go with a very simple array.
You have to modify your view controllers interface definition to tell the program that your view controller can provide data and delegation to a picker view.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
Than just implementing a couple of methods will make it work however documentation should be checked for other methods that are optional but provides more customization and control.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
NSArray *options;
- (void)viewDidLoad
{
[super viewDidLoad];
options = #[#"a",#"b",#"c",#"d"];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [options count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [options objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(#"%# selected.",[options objectAtIndex:row]);
}

Dynamically Add values to UIPickerView at run time

How can you dynamically add values to UIPickerView at runtime.
I'm using the following code to populate a UIPickerView statically. Need to add
values dynamically at run time, for e.g. Three, Four etc.
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *title = nil;
if(row==0){
title = #"One";
}
if(row==1){
title = #"Two";
}
If you had an array of the titles, you could use something like:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [titles objectAtIndex:row];
}
The titles will be refreshed when you call [pickerView reloadAllComponents] (or reloadComponent: if you have more than one column and only want to refresh one of them).
Hey I had a problem with UIPickerView..... when I call reloadAllComponents it wasn't calling widthForComponent, but it was calling other delegate methods such as titleForRow.... I have no idea why this is, perhaps apple doesn't want the width changing dynamically? anyway it was on another screen so I didn't have the animation/no-animation issue. I found that by resetting the delegate of the picker, I could get widthForComponent to be called again!
i.e.
picker.delegate = view;
where view implements UIPickerViewDelegate.... hope this helps someone out there!