Back Button on UIWebView - ios

I'm trying to figure out how to create a back button that allows the user to go back one page. I read through Apple's Docs (which still go way over my head) and found out that I need to setup the canGoBack and goBack's. I have tried this, but for some reason it is not working. My UIWebView is named viewWeb, and I created and attached an outlet to my Back button, named backButton, and also tagged it as 1. Here is my code that I wrote in the View Controller:
// Back button:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[_backButton addTarget:_viewWeb
action:#selector(goBack)
forControlEvents:UIControlEventTouchDown];
if ([_viewWeb canGoBack]) {
NSLog(#"Back button pressed.");
[_viewWeb goBack];
}
}
else return;
}
Does anyone know what I need to change / add to get this working?

actionSheet:clickedButtonAtIndex: is for UIActionSheet objects, not UIButton actions.
You should probably write an IBAction method that looks something like this:
- (IBAction)backButtonTapped:(id)sender {
[_viewWeb goBack];
}
and connect it to the Touch Up Inside action from the button.
You can search for more info on IBAction but that's likely what you want

For swift 4 and swift 5
#IBAction func refreshAction(_ sender: Any) {
self.webView.reload()
}
#IBAction func backAction(_ sender: Any) {
if(self.webView.canGoBack) {
self.webView.goBack()
}
}
#IBAction func forwardAction(_ sender: Any) {
if self.webView.canGoForward{
self.webView.goForward()
}
}

I think it should be more specific like this
- (IBAction)backButtonTapped:(id)sender {
if ([_viewWeb canGoBack] ) {
[_viewWeb goBack];
}
}

I have struggled to get a working code for this function written in swift and finally here's what i came up with.
#IBOutlet weak var goBackBtn: UIBarButtonItem!
#IBOutlet weak var goForwardBtn: UIBarButtonItem!
#IBOutlet weak var itemWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL (string: "www.google.com")
let requestObj = NSURLRequest(URL: url)
itemWebView.loadRequest(requestObj)
itemWebView.delegate = self
goBackBtn.enabled = false
goForwardBtn.enabled = false
}
func webViewDidFinishLoad(webView: UIWebView) {
goBackBtn.enabled = itemWebView.canGoBack
goForwardBtn.enabled = itemWebView.canGoForward
}
#IBAction func forward(sender: AnyObject) {
itemWebView.goForward()
}
#IBAction func back(sender: AnyObject) {
itemWebView.goBack()
}

Related

How to Handle IQKeyboardManager Done button action in toolbar?

I am new in ios I work on iqkeyboardmanager and I want to access Done button action in IQKeyboardManager.
You can handle clicks of done, next and previous button
[textField.keyboardToolbar.previousBarButton setTarget:self action:#selector(previousAction:)];
[textField.keyboardToolbar.nextBarButton setTarget:self action:#selector(nextAction:)];
[textField.keyboardToolbar.doneBarButton setTarget:self action:#selector(doneAction:)];
In swift:
customField.keyboardToolbar.doneBarButton.setTarget(self, action: #selector(doneButtonClicked))
func doneButtonClicked(_ sender: Any) {
//your code when clicked on done
}
you can use UITextViewDelegate
func textFieldDidEndEditing(_ textField: UITextField) {
}
You can import
import IQKeyboardManager
in required file and after that
vc1Textfield.addDoneOnKeyboard(withTarget: self, action: #selector(doneButtonClicked))
here vc1Textfield is my textfield and doneButtonClicked definition is given below:
#objc func doneButtonClicked(_ sender: Any) { }
Hope it help someone!!! Happy Coding....
I will try to describe in a more convenient way:
import IQKeyboardManagerSwift
class EditPostViewCell: UITableViewCell {
#IBOutlet weak var commentTextView: UITextView!
override func awakeFromNib() {
IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "Send Comment"
commentTextView.delegate = self
}
#objc func didPressOnDoneButton() {
commentTextView.resignFirstResponder()
sendComment()
}
}
extension EditPostViewCell: UITextViewDelegate {
public func textViewDidBeginEditing(_ textView: UITextView) {
let invocation = IQInvocation(self, #selector(didPressOnDoneButton))
textView.keyboardToolbar.doneBarButton.invocation = invocation
}
}
First:
import the IQKeyboardManager.h
Then:
[self.textField.keyboardToolbar.doneBarButton setTarget:self action:#selector(doneAction:)];
xcode 11.
Make Action connection textField with code.
#IBAction func yourName(_ sender: Any){
// Your code
}
Connections inspector screen

Is hidden not working on image views in Swift 3?

#IBOutlet weak var allStocksSelected: UIImageView!
#IBOutlet weak var shortStocksSelected: UIImageView!
#IBOutlet weak var longStocksSelected: UIImageView!
#IBOutlet weak var myStocksTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
myStocksTableView.delegate = self
myStocksTableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
self.shortStocksSelected.isHidden = true
self.longStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
}
#IBAction func allStocksButtonPressed(_ sender: Any) {
print("ALL!")
self.stockTypeChanged(stockType: allStocksSelected)
}
#IBAction func shortStocksButtonPressed(_ sender: Any) {
print("SHORT!")
self.stockTypeChanged(stockType: shortStocksSelected)
}
#IBAction func longStocksButtonPressed(_ sender: Any) {
print("LONG!")
self.longStocksSelected.isHidden = false
self.shortStocksSelected.isHidden = true
self.allStocksSelected.isHidden = true
//stockTypeChanged(stockType: longStocksSelected)
}
#IBAction func addNewStockButtonPressed(_ sender: Any) {
}
#IBAction func signOutButtonPressed(_ sender: Any) {
}
private func stockTypeChanged(stockType: UIImageView) {
let stockTypes: [UIImageView] = [allStocksSelected, shortStocksSelected, longStocksSelected]
for (stock) in stockTypes {
if (stock == stockType) {
stock.isHidden = false
} else {
stock.isHidden = true
}
}
}
As shown from my code above, I am basically just trying to hide and show certain image views when buttons are pressed.
I am 100% sure that all of the buttons actions and IB outlets are properly connected, yet the image views are still not hiding and showing, and I cannot figure out why.
I was running into this same issue. Placing your isHidden code to execute on the main thread should solve the problem.
DispatchQueue.main.sync {
}

Using same button to Play/Pause and loop audio in AVFoundation

I have tried linking the action to the same button but this stops the button from working altogether. I can get it working using the same code if I create a separate button:
#IBAction func Play(sender: AnyObject) {
SoundPlayer.play()
}
#IBAction func stop(sender: AnyObject) {
SoundPlayer.stop()
}
I also need the audio to loop.
Using Nirav code:
class ViewController: UIViewController {
var SoundPlayer:AVAudioPlayer = AVAudioPlayer()
#IBOutlet var btnPlay: UIButton!
#IBOutlet var btnStop: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let FileLocation = NSBundle.mainBundle().pathForResource("Aspirin", ofType: ".mp3")
do {
SoundPlayer = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: FileLocation!))
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
try AVAudioSession.sharedInstance().setActive(true)
}
catch {
print(error)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func btnTap(sender: UIButton) {
if (sender == self.btnPlay) {
}
else if (sender == self.btnStop) {
}
}
}
1 - Screenshot of storyboard
You can do that just create IBOutlet of both button like this
#IBOutlet var btnPlay: UIButton!
Now bind this action to both button
#IBaction func btnTap(sender: UIButton) {
if (!sender.selected) {
SoundPlayer.play()
}
else {
SoundPlayer.stop()
}
sender.selected = !sender.selected
}
Hope this will help you

How to show and hide a view on button click

I have one uiview , Button .At initial my view will be hidden So when my button click I need to show my uiview and when I click same button I need to hide same view.
How to do that in swift 2.0.Now what I did is when I click first time - its showing.
#IBAction func PressRefine(sender: AnyObject) {
menuView.hidden = false
}
But again when I press it should hide.
How to do that???
Try an if statement.
#IBAction func PressRefine(sender: AnyObject) {
if menuView.hidden {
menuView.hidden = false
} else {
menuView.hidden = true
}
}
or as #TedHuinink suggested, with less code.
#IBAction func PressRefine(sender: AnyObject) {
menuView.hidden = !menuView.hidden
}
first make view outlet
#IBOutlet weak var newpptview: UIView!
#IBAction func newpresentation(_ sender: Any) {
if newpptview.isHidden{
newpptview.isHidden = false
} else{
newpptview.isHidden = true
}
}
its better that to all
OR
#IBAction func newpresentation(_ sender: Any) {
newpptview.isHidden = !newpptview.isHidden
}
swift 3 and 4

Hide a view container with a button in the ViewContainer

I have a View. In this view, I have a Container View. And in the ContainerView I have a button.
When I am touching the button of the ContainerView, I want the ContainerView become hidden.
I want to do something like that :
class ContainerView: UIViewController {
#IBAction func closeContainerViewButton(sender: AnyObject) {
//I try this : self.hidden = false
//or this : self.setVisibility(self.INVISIBLE)
}
}
Any idea how do it?
There are serval ways but here is the easiest one, not prettiest though. You should really use delegates but this is a hacky way to get started. Just create a global variable of the class that holds the container (startController in this case). Then call it from your other view controller (MyViewInsideContainer) and tell it to hide the view you´re in. I have not run this code but it should work.
var startController = StartController()
class StartController:UIViewController {
#IBOutlet var myViewInsideContainerView: UIView
....
override func viewDidLoad() {
super.viewDidLoad()
startController = self
}
func hideContainerView(){
self.myContainerView.hidden = true
}
}
class MyViewInsideContainer:UIViewController {
...
#IBAction func hideThisView(sender: AnyObject) {
startController.hideContainerView()
}
}
i think a cleaner solution is to use delegation:
in the ParentViewController
class ParentViewController: UIViewController ,ContainerDelegateProtocol
{
#IBOutlet weak var containerView: UIView!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
//check here for the right segue by name
(segue.destinationViewController as ContainerViewController).delegate = self;
}
func Close() {
containerView.hidden = true;
}
in the ContainerViewController
protocol ContainerDelegateProtocol
{
func Close()
}
class ContainerViewController: UIViewController {
var delegate:AddTaskDelegateProtocol?
#IBAction func Close(sender: AnyObject) { //connect this to the button
delegate?.CloseThisShit()
}

Resources