odd behavior of UIPickerView on ActionSheet - objective-c

- (void) actionsheetstart
{
actionSheet=[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.view];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 480, 32)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 216);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[actionSheet addSubview:picker];
}
-(void)done_clicked:(id)sender
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancel_clicked:(id)sender
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
for some reason, the actionsheet is fine, but the UIPickerView and the UIToolBar gone crazy.
I've try to set the frame to -px but without any success. what seems to be the problem?
Thanks alot.

Don't insert UIPickerView inside UIActionSheet, you won't get correct behaviour this way. My solution:
Subclass UIButton, implement method canBecomeFirstResponder:, return YES.
Implement inputView method in your subclassed UIButton, return UIPickerView.
Optionally, implement inputAccessoryView method in your subclassed button to return toolbar above your picker.
Make your button becomeFirstResponder: when tapped.
Voila, you have picker shown modally like a keyboard when button is tapped.
EDIT:
MyButton.h
#import <UIKit/UIKit.h>
#interface MyButton : UIButton
#end
MyButton.m
#import "MyButton.h"
#implementation MyButton {
UIDatePicker *_pv;
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
#pragma mark - Picker
- (UIView *)inputView {
// set up your UIPickerView here
_pv = [[UIDatePicker alloc] init];
[_pv setDatePickerMode:UIDatePickerModeTime];
[_pv setMinuteInterval:30];
return _pv;
}
- (UIView *)inputAccessoryView {
// set up your toolbar here
UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *ok = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pick:)];
[tb setItems:[NSArray arrayWithObjects:flex, ok, nil]];
return tb;
}
- (void)pick:(id)sender {
// store picker value here before closing inputView
[self resignFirstResponder];
}
#end

Related

UIBarButtonItem target/action not working

I am implementing a view with a date picker and a toolbar.
I have the following code:
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), 44)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(didSelectCancelButton)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(didSelectDoneButton)];
NSArray *buttons = #[cancelButton, flexible, doneButton];
[self.toolbar setItems:buttons
animated:NO];
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(frame), 216)];
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];
}
return self;
}
- (IBAction)didSelectCancelButton {
if ([self.delegate respondsToSelector:#selector(datePickerDidCancelDateSelection:)]) {
[self.delegate datePickerDidCancelDateSelection:self];
}
}
- (IBAction)didSelectDoneButton {
NSLog(#"");
}
But when I click the buttons, no action is performed. The methods are not invoked.
Can you tell what am I doing wrong?
Thanks
EDIT:
Turned out there was a gesture recognizer that was capturing the touch events.
Fixing that resolved this problem.
I suspect your date picker (which is as wide as the toolbar is) is somehow inhibiting your buttons from getting user touches.
Try reversing these two lines to this:
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];
Do you have any UIGestureRecognizer objects, if you do try to disable them and test.

how do you add a right button to a uinavigationcontrol

How can I add a button to a UINavigationControl that I added to a ViewController? (The project isn't UINavigationControler based.)
Here's the code I've added to my viewDidLoad method:
navigation = [[UINavigationController alloc] init];
navigation.view.frame = CGRectMake(0, 0, 1024, 748);
navigation.navigationBar.tintColor = [UIColor blackColor];
navigation.navigationBar.alpha = 1.0f;
navigation.navigationBar.translucent = NO;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
[navigation.navigationItem setRightBarButtonItem:cancelButton animated:YES];
[cancelButton release];
[self.view addSubview:navigation.view];
[self pushPresentationList];
Can someone help me fix lines 7-9 please?
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Button"
style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Awesome"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[navigationBar pushNavigationItem:item animated:NO];
Do you need an UINavigationController? Why not just use a simple UINavigationBar. Here is some sample code:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:
CGRectMake(0,0,self.view.frame.size.width, 49.0f)];
[self.view addSubview:navBar];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
UINavigationItem *rightBarButtonItem = [[UINavigationItem alloc] init];
[rightBarButtonItem setRightBarButtonItem:cancelButton];
[navBar pushNavigationItem:rightBarButtonItem animated:NO];

hidden navigation bar causing trouble with action sheet

The navigation bar in my uinavigationbarcontroller is hidden, but my actionsheet thinks its still there, hence giving me a the highlights at the top of the action page (seen above). How can I erase this/ trick my actionsheet into thinking its a normal uiimageview?
Navigation bar hidden like so in didLoad:
[self.navigationController setNavigationBarHidden:YES animated:NO];
and:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (![self.navigationController isNavigationBarHidden])
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
}
actionsheet looks like this in didLoad:
actionSheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 200,320,16)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
UIPickerView *locationPicker = [[UIPickerView alloc] init];
locationPicker.frame = CGRectMake(0, 244, 320, 216);
locationPicker.delegate = self;
locationPicker.dataSource = self;
locationPicker.showsSelectionIndicator = YES;
locationPicker.tag = 1;
[actionSheet addSubview:locationPicker];
Then its animated in on textfield touch.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == locationTextField.self)
{
[actionSheet showInView:self.view];
[actionSheet setFrame:CGRectMake(0, 0, 320, 480)];
[nameField resignFirstResponder];
}
}
I believe the problem is with the view you chose to show the action sheet. try to show it in your app's main window.

UIActionSheet on iPad frame too small

I am trying to display a UIActionSheet on the iPad with a UIPickerView in it, I have the equivalent code working for iPhone so my UIPickerView delegate and datasource stuff work, but on the iPad when I use -[UIActionSheet showFromRect:inView:animated:] the result UIPopover/UIActionSheet is way too small and I can not seem to set the frame size, also none of the buttons are displayed.
I don't know if this is because they are outside the bounds or there is something else going on. This is what my code looks like after I have removed all non-essential code (iPhone etc). Does anybody know what I am doing wrong, does anybody know of any examples.
CGRect thePickerFrame = CGRectMake(0, 0, 320.0, 485.0);
UIPickerView * thePickerView = [[UIPickerView alloc] initWithFrame:thePickerFrame];
[pickerActionSheet release], pickerActionSheet =
[[UIActionSheet alloc] initWithTitle:#"Choose" delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Next", nil];
thePickerView.showsSelectionIndicator = YES;
thePickerView.dataSource = self;
thePickerView.delegate = self;
[pickerActionSheet addSubview:thePickerView];
[thePickerView selectRow:0 inComponent:0 animated:NO];
[thePickerView release];
[pickerActionSheet showFromRect:currentTextField.bounds
inView:currentTextField animated:NO];
pickerActionSheet.frame = thePickerFrame;
I think that UIActionSheet is not resizable, try to comment in your code the line with [pickerActionSheet addSubview:thePickerView]; and you will see that the ActionSheet fits perfecly to the buttons.
I would recommend a UIPopoverController with a custom UIViewController. Something like this:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStyleBordered target:self action:#selector(BACK_ACTION:)];
UIBarButtonItem *chooseButton = [[UIBarButtonItem alloc] initWithTitle:#"Choose" style:UIBarButtonItemStylePlain target:nil action:nil];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(NEXT_ACTION:)];
UIBarButtonItem *fixed1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fixed2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbar setItems:[NSArray arrayWithObjects:cancelButton, fixed1, chooseButton, fixed2, nextButton, nil]];
UIPickerView *thePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
CGRect thePickerFrame = thePickerView.frame;
thePickerFrame.origin.y = toolbar.frame.size.height;
[thePickerView setFrame:thePickerFrame];
UIView *view = [[UIView alloc] init];
[view addSubview:thePickerView];
[view addSubview:toolbar];
UIViewController *vc = [[UIViewController alloc] init];
[vc setView:view];
[vc setContentSizeForViewInPopover:CGSizeMake(320, 260)];
popover = [[UIPopoverController alloc] initWithContentViewController:vc];
thePickerView.showsSelectionIndicator = YES;
thePickerView.dataSource = self;
thePickerView.delegate = self;
[thePickerView selectRow:0 inComponent:0 animated:NO];
[popover presentPopoverFromRect:currentTextField.bounds inView:currentTextField permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Where popover is a class variable declared in .h (UIPopoverController *popover;).
By the way, I'm using ARC, so there is no release in the code.
I managed to make it work using a silly way.
For your reference:
Setup PickerView.
UIActionSheet's width can't be adjusted somehow, so have to adjust pickerView accordingly. Height wise, you can adjust with the amount of "\n" in actionSheet title.
UIDatePicker * pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 270, 220)];
pickerView.datePickerMode = UIDatePickerModeDate;
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:#"\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet addSubview:pickerView];
[actionSheet showFromRect:button.frame inView:button.superview animated:YES];
[actionSheet release];
Set Frame or Bound doesn't work for me.
[actionSheet setBounds:pickerView.frame];
[actionSheet setFrame:pickerView.frame];
Agreed on using UIPopoverController, but don't know why, it got a serious UI delay when I put UIDatePickerView into popover (it took almost 1 sec lag to pop up) which I can't find the root cause. So have to fallback to above method.
Happy Coding.

Add action to UIButton which in a customview

I have a custom view and it has a UIButton. I add this view to my navigationItem's rightbarButtonItem, it's ok, but I have to add an action to this button.
Here is my code;
-(void)showMessage:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"next" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
}
- (void)viewDidLoad {
UpDownButtonView *buttonView = [[UpDownButtonView alloc] initWithNibName:#"UpDownButtonView" bundle:nil];
[buttonView.upButton addTarget:self action:#selector(next:) forControlEvents:UIControlEventTouchUpInside];
[buttonView.downButton addTarget:self action:#selector(prev:) forControlEvents:UIControlEventTouchUpInside];
buttonView.view.frame = CGRectMake(0,0,95,34);
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:buttonView.view];
self.navigationItem.rightBarButtonItem = item;
[self setTitle:#"Details"];
[super viewDidLoad];
}
Thanks.
You don't need a custom view with a UIButton, what you need is alloc and init a UIBarButtonItem and put it on your nav bar
the code would look like:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Back", #"Back Button") style:UIBarButtonItemStylePlain target:self action:#selector(doneButtonPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];