actionButton not working in XCODE - ios

I have this block of code in my xcode project. I am having trouble being able to click the button and to make this work. The button displays in my simulator but does not work when I click it. Does anyone have any ideas on what is going on here? It was working at one point but now seems to have failed. Maybe I accidentally changed a build setting? Or simulator setting? Any thoughts would be appreciated!!
Code for the button
-(void)viewDidLoad{
[super viewDidLoad];
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self action:#selector(openWebLink)];
NSArray *items = [NSArray arrayWithObjects: actionButton, nil];
[self.toolbar setItems:items animated:NO];
[actionButton release]; }
Here is the code for the action:
-(void)openWebLink{
// open a dialog with an OK and cancel button
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Do you want to apply for this job?"
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"OK" otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showFromToolbar:_toolbar];
[actionSheet release]; }

Related

Prevent leftBarButtonItem navigationItem show alert

I have a text editing view, and I would like to prompt the user before returning to the rootview when the text has been edited.
I have tried this so far.
self.navigationItem.leftBarButtonItem.title = #"Back";
self.navigationItem.leftBarButtonItem.tintColor = [UIColor grayColor];
-(void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
// back button was pressed. We know this is true because self is no longer
// in the navigation stack.
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Confirm Submission" message:#"Current Job Sheet Incomplete\n Please Confirm Your Submission" delegate:self cancelButtonTitle:#"Submit" otherButtonTitles:#"Cancel", nil];
alert.tag = 1;
[alert show];
}
// [super viewWillDisappear:animated];
}
Although this shows the alert, it dose not stop the user from being pushed to the root view.
Add a custom button on leftBarButton
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style: UIBarButtonItemStylePlain target:self action:#selector(navigationBackBtnTap)];
self.navigationItem.leftBarButtonItem = backButton;
-(void)navigationBackBtnTap{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Confirm Submission" message:#"Current Job Sheet Incomplete\n Please Confirm Your Submission" delegate:self cancelButtonTitle:#"Submit" otherButtonTitles:#"Cancel", nil];
alert.tag = 1;
[alert show];
}

UIActionSheet With PickerView not showing up in iPad

I am using the following code to add an actionsheet with a picker view on it to my app. From the iPhone, it displays perfectly, but when viewing on iPad (Universal app), it shows a very small pickerview with none of the other buttons right on the center of the page.
UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:#"Select Chapter"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Select"
otherButtonTitles:nil];
// Add the picker
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,185,0,0)];
pickerView.delegate = self;
pickerView.showsSelectionIndicator = YES; // note this is default to NO
[menu addSubview:pickerView];
[menu showFromTabBar:self.tabBarController.tabBar];
[menu setBounds:CGRectMake(0,0,320, 700)];
[pickerView release];
[menu release];

Closing UIActionSheet

I have a UIActionSheet and I have added the following - UIPickerView and 2 BarButtonItems. I am trying to close the action sheet when someone clicks the Cancel Button (it is one of the bar button items).
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0,40,0,0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
UIToolbar *tools = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
tools.barStyle = UIBarStyleBlackOpaque;
[actionSheet addSubview:tools];
UIBarButtonItem *doneButton=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinDoneClicked)];
doneButton.imageInsets=UIEdgeInsetsMake(200, 6, 50, 25);
UIBarButtonItem *CancelButton=[[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(btnActinCancelClicked)]; //this is where the problem is
NSArray *array = [[NSArray alloc]initWithObjects:CancelButton, doneButton, nil];
[tools setItems:array];
[actionSheet showFromTabBar:self.tabBarController.tabBar];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
I have added another method to dismiss the action sheet -
-(IBAction)btnActinCancelClicked:(id)sender{
[sender dismissWithClickedButtonIndex:0 animated:YES];
}
The problem is that the app crashes when the BarButton is clicked. I think the problem is caused where the CancelButton is declared and it is unable to send a message to the btnActinCancelClicked method, but I can't figure out how to fix it.
Thanks
Your method is this;
-(IBAction)btnActinCancelClicked:(id)sender
So you need to add it like this (because it receives a parameter);
#selector(btnActinCancelClicked:)
So for example, if a method had three parameters you'd call it #selector(myMethod:andX:andY:)
Also, to access your UIActionSheet (not entirely sure this will work);
UIActionSheet *acSheet = sender.superView;
//Check if it is the ActionSheet here then dismiss it
[acSheet dismissWithClickedButtonIndex:0 animated:YES];
It's not the sender you want to dismiss. It's the ActionSheet. You'll need a reference for that and change your code for something like:
- (IBAction)btnActinCancelClicked:(id)sender{
[self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

Is having a UIPickerView in a UIActionSheet acceptable?

Is it acceptable (aka would Apple consider it acceptable) to have a UIPickerView in a UIActionSheet?
Yes, It is totally acceptable. To some degree its even encouraged.
Example code: HERE by Erica Sadun in her book iPhone Developer's cookbook. Chapter 11 Recipe 21
Yes. It is acceptable.
set the frame of the UIPickerView
add the UIPickerView to the actionView
As I remember, actionView will be full screen in this case
UIActionSheet *actionView = [[UIActionSheet alloc] initWith...];
UIPickerView *pickerView = [UIPickerView alloc] init...];
pickerView.frame = CGRect(....);
[actionView addSubview:pickerView];
[pickerView release];
[actionView showInView:theView];
[actionView release];
This should do it :
NSString *title = #"\n\n\n\n\n\n\n\n\n\n\n\n"; // <--- Taken from Erica Sadun´s CookBook
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[actionSheet setBounds:CGRectMake(0,0,320,485)];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
[picker setDataSource:self];
[picker setDelegate:self];
[picker setShowsSelectionIndicator:YES];
[actionSheet addSubview:picker];
[actionSheet showFromTabBar:[[self tabBarController] tabBar]];
Please remember that you have to set UIPickerView´s dataSource and delegate .

UITextfield mimic Dropdown (UITableView?)

I want to add the feature that when i tap on a specific textfield, then there is a Dropdown / TableView in front of the gui and i can select a value and then the selected value is then inserted into the textfield?
How to achieve this in the easiest way?
Depends, if this is on the iPad, you can use UIPopoverController and present a UITableViewController inside it.
If not, you can present a UIActionSheet and add UIPickerView or a UITableView inside it...in this example, I am creating a UIActionSheet and placing a UIPickerView inside it:
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? #"\n\n\n\n\n\n\n\n\n" : #"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:[NSString stringWithFormat:#"%#%#", title, [entry objectForKey:#"Name"]]
delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:#"Ok", nil];
[actionSheet setDelegate:self];
[actionSheet setTag:row];
pv = [[UIPickerView alloc] init];
[pv setShowsSelectionIndicator:YES];
[pv setDelegate:self];
[pv setTag:28];
[actionSheet addSubview:pv];
[actionSheet showInView:[[[self navigationController] tabBarController] view]];
[pv release];
Notice the "\n"s, you need those to make room for the Picker view. Here's the result:

Resources