iPhone UIWebView back button - iphone

I am trying to make a back button in my navigation bar because i already have a tab bar at the bottom of the screen.
my layout is this:
tab bar[
navigation bar {
webview ()
}
]
essentially. i need to programmatically add a back button and cannot seem to figure out how. i know that there is a goBack method but am not very familiar with how to implement this.
I already have a button which pulls up ana action sheet with several options but how would i go about having that use the goBack method?
as I understand I can also use something like
if (mywebview canGoBack) {
[mywebview goBack]
}
but I'm not sure how to make an action sheet button do this.
any help?

Somewhere in your action sheet's delegate file, you should have a method similar to this:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { // if the first option is selected, whatever it may be... (counting starts at zero))
if ([myWebView canGoBack]) {
[myWebView goBack];
}
}
else return;
}

Related

How to pass UIAction without touching the UIButton

I've got a UIButton that acts as a switch.
When user taps on it, its state changes to "selected" and action "one" is called. When user taps again UIButton state changes to "not selected" and the action is no longer available.
Is there a way to set the UIButton to "selected" by taping on a completely different UIButton and have it change to "selected" and call the same action as well?
Cheers
From what I have understood from the question, I would like to give you my suggestion
if (!<your_button>.selected)
{
<your_button>.selected = YES;
// do your stuff here
}
else
{
<your_button>.selected = NO;
// do your stuff here, in your case action should be no longer available so that,
<your_button>.userInteractionEnabled = NO;
}
Enjoy Programming !
try like this,'
-(IBAction)nextbutton:(id)sender{
UIButton *btn=(UIButton *)[self.view viewWithTag:tagValue];//pass tag value of that particuler button
btn.selected=YES;
//do whatevr you want.
}
simple thing when you tap on the uibutton make an action for that and in the action for that button , you can set the button state to selected and then in the next line of code you can call that method (action)
- (IBAction)btn1Touched:(id)sender {
[_btn2 setSelected:YES];
[self btn2Touched:self];
}
- (IBAction)btn2Touched:(id)sender {
NSLog(#"btn Action called");
}
UIbutton has state called selected and it is managed via property selected
so to make a button to selected state
[buttonInstance setSelected:YES];
To make it unselected use
[buttonInstance setSelected:NO];
Kindly write a function for that button action like,
-(void)Buttonaction
{
do your action here
also set your button has selected state.
buttonname.setselected=yes;
}
call this function while you are tabbing your button.
Do This:
-(IBAction)switchButtonClicked:(UIButton*)sender
{
if(sender.isSelected)
{
.....
}
else
{
...
}
[sender setSelected:!sender.isSelected];//This will work as Switch.
}
set Selected(Switch ON) and default(Switch OFF) property (image and title) of UIButton from XIB, OR from Code as you want.
With a single button you can do you functionality with this code

iOS Determine button that was programmatically created

I have made a horizontal scroll view with images and buttons in it using a for loop, what i want to do now is when a button is pressed, open the image it corresponds to full screen. The issue im having is determining which button has been pushed. I am using :
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then :
-(void)buttonPressed:(UIButton *)sender {}
What can i do to fix this? Thanks
Which button has been pushed
Create a property to store a tag. In the buttonPressed method set the property to sender.tag
Now you can know which was the button who got pressed last.
The sender is the button that the user has tapped, so when you create the buttons you could use the tag property and set it to an index of an array where you hold your images. But this could be a bit unstable if you change the order or amount of images or buttons for example so be prepared to check for that.
one way to do so is to keep references of your button (with a property, an attribute in your class) and test if it is the good one in your buttonPressed method
- (void)buttonPressed:(UIButton *)sender {
if (sender == self.myButton) {
// DO YOUR WORK HERE
}
}
you can also create a method for this and only this one button
by the way it is better to say
- (IBAction)buttonPressed:(UIButton *)sender
you then can set the target of your button in the interface builder interface
set tag for each button in the for loop but.tag=i;
-(void)buttonPressed:(UIButton *)sender {
if (sender.tag==1){
//display image 1
}
else if ....
}

Removing cancel button in UIImagePickerController?

I am developing an app for ios 5.
I need to remove the cancel button on UIImagePickerController i searched for this problem on the forum but didnt get exact answer can someone please help me with this?
That is because it is not possible to remove that cancel button. That is an inbuilt function and you can not make changes in the same.
Swift Version, compatible 4+
To remove the navigation bar :
imagePicker.view.subviews
.filter { $0.isKind(of: UINavigationBar) }
.forEach { $0.isHidden = true }
for removing the buttons only :
imagePicker.view.subviews
.filter { $0.isKind(of: UIButton) }
.forEach { $0.isHidden = true }
VoilĂ 
I will give the best method to achieve this:
First create a subclass of the UiImagePickerController
in the subclass respond to the UINavigationBarDelegate method
- (BOOL)navigationBar:(UINavigationBar *)navigationBar
shouldPushItem:(UINavigationItem *)item
you dont have to do anything like setting the delegate or adding a protocol, just override the method inside your custom UIImagePcikerController.
in this method return NO for the cancel item (which is the first one)
I did this with ELCImagePickerController
There isn't a way to remove only the cancel button. UIImagePickerController exposes a property called showCameraControls, which will hide the bottom bar with the cancel button and the camera button, as well as the controls for flash, HDR, and flip camera, giving you just the camera preview.
If you want to provide an experience without a cancel button, you'll have to create a camera overlay view of what you want.
Assuming you have code invoking UIImagePickerController, you can turn off camera controls like this:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setShowsCameraControls:NO];
Assuming you'll overlay it with your own view without the cancel button, you'll add this (assuming you have a UIView called cameraOverlay:
[imagePicker setCameraOverlayView:cameraOverlay];
This will hide Navigationbar itself along with the Cancel and Title
let videoPicker = UIImagePickerController()
for view in videoPicker.view.subviews {
if let navBar = view as? UINavigationBar {
navBar.isHidden = true
}
}
If you want to remove the cancel button alone, dig deep into navBar
When you present the UIImagePickerView try puting the below code
for (UIView *subview in view.subviews) {
NSLog(#"subviews=%#",subview);
NSString *className = [NSString stringWithFormat:#"%#", [subview class]];
}
By the above code you can get the navigation controller used for displaying the cancel button.. Once you get the navigationController Set its leftside button to nil..
I have not used it but hope it can be of help to you

Rename standard "edit" button in tableview

I managed (with lots of trial and error) to have my tableview provide only reordering functionality, i.e. my tableview is editable but does not display "delete icons" nor indents the rows upon clicking on the edit button.
Now I would like the button to read "sort" instead of "edit".
I naively tried this:
self.navigationItem.leftBarButtonItem = self.editButtonItem;
self.navigationItem.leftBarButtonItem.title = #"Sort";
which works only once, i.e. it is correctly labeled "Sort", once clicked it renames to "Done", but then--as expected--it re-renames to "Edit".
In order to fix this, I deployed my "own" button on the navbar. This solution works--I can get the button to control the tableview editing mode, reload the data upon change, rename itself, etc--but I cannot get it to "stay highlighted", i.e. the default behaviour of the "Edit" button in a tableview.
Now my question is either:
a) Is there a way to rename (and keep it renamed, e.g. through a callback) the standard "Edit" button?
or
b) Is there a way to have a button behave "modally", i.e. stay selected, like the standard "Edit" button?
Thanks for any idea you might have.
You can put your changes in the - (void) setEditing:(BOOL)editing animated:(BOOL)animated method in your view controller.
- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
//Do super before, it will change the name of the editing button
[super setEditing:editing animated:animated];
if (editing) {
self.navigationItem.leftBarButtonItem.title = #"Done";
self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
}
else {
self.navigationItem.leftBarButtonItem.title = #"Sort";
self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleBordered;
}
}

Different actions on alert views buttons, depending on the alert view

I have 3 alert views in my apps.
'wonAlert'
'lostAlert'
'nagAlert'
I implement this to give them actions.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
This used to work fine, when I had just the 'wonAlert' and 'lostAlert', they had a dismiss and a learn more button that took them to wikipedia,
now I want the nag alert to take them to the app store.
how can I make it so the above method knows which alert view the tap is coming from, or something like that?
Cheers, Sam
It sounds like you've got the UIAlertViews in variables, so I'd use them:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView == wonAlert) {
//DO STUFF
}
else if (alertView == lostAlert) {
//DO OTHER STUFF
}
else if (alertView == nagAlert) {
//OPEN APP STORE
}
}
More than one view can have the same tag and you can easily mistype the title or change it and forget to update the delegate method.
In your view controller header file, add <UIAlertViewDelegate> so that it agrees to handle UIAlertView delegate methods:
#interface MyViewController : UIViewController <UIAlertViewDelegate> { ... }
In the implementation of your view controller, add the following delegate method:
- (void) alertView:(UIAlertView *)_actionSheet clickedButtonAtIndex:(NSInteger)_buttonIndex {
if ([_actionSheet.title isEqualToString:#"titleOfMyAlertView"]) {
if (_buttonIndex == 0) {
// do stuff for first button
}
else if (_buttonIndex == 1) {
// do something for second button
}
// etc. if needed
}
}
The _actionSheet.title property can be used to distinguish between alert views. My recommendation is to use NSString constants or NSLocalizedString(), if you have a localized strings table, to title your alert views.
I answered a similar question here:
Alert with 2 buttons
the correct way to do this is using the tag property of alerts
upon creating each alert, set its tag variable by adding:
alertName.tag = #; //ex: alertName.tag = 1
Then, in the clickedButtonAtIndex method, you will need to add an 'if' block for each alert you have as shown in the folllowing code:
if(alert.tag == 1)
{
if (buttonIndex == 0)
{
//do stuff
}
else
{
//do other stuff
}
}
if(alert.tag == 2)
///...etc
I would do what Alex suggests, but use the tag property of the AlertView to determine which AlertView had been used.