Button triggers another view - iphone

Right now I have this:
lazy var Button: UIButton = {
let tb = UIButton()
tb.backgroundColor = UIColor.yellowColor()
tb.clipsToBounds = true
tb.layer.cornerRadius = 39
tb.addTarget(self, action: #selector(showButton), forControlEvents: .TouchUpInside)
tb.setBackgroundImage(UIImage(named: "buttonMenu"), forState: UIControlState.Normal)
return tb
}()
How can I make the button take me to another view or do other stuff like opening the camera?
PS: The showButton function doesn't do anything really.

So you have
tb.addTarget(self, action: #selector(showButton), forControlEvents: .TouchUpInside)
but you need
tb.addTarget(self, action: #selector(className.showButton), forControlEvents: .TouchUpInside)
func showButton(sender:UIButton) {
//Code
}

I have tried this in 7.2.1 Xcode
import UIKit
import CoreLocation
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let Button: UIButton = {
let tb = UIButton()
tb.frame = CGRectMake(0, 0, 100, 50)
tb.backgroundColor = UIColor.yellowColor()
tb.clipsToBounds = true
tb.layer.cornerRadius = 39
tb.addTarget(self, action: "showButton:", forControlEvents: .TouchUpInside)
// only change the action syntax like "action: "showButton:" "
return tb
}()
self.view .addSubview(Button)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func showButton(sender:AnyObject?) {
//Code
}
}

Related

#selector(didTapRegister) not in scope

When I run this code by #selector(didTapRegister) I get a didTapRegister is not in scope. I have a bigger application I am using it in and on one page it works and another it won't, well it won't work anywhere else.
I am stuck on figuring out why. Can anyone help solve it? seems to be an Objective-C issue possibly because I have same issues trying to make IBOutlets, I have tried searching for answers and I just don't have enough knowledge to understand why it isn't working, I've tried moving the code to different scopes and still nothing. with the IBOutlets it's saying IBOutlet property cannot have non-object type UIButton?.Type
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let Button = UIButton.init(type: .custom)
Button.setTitle("Chat", for: .normal)
Button.layer.backgroundColor = CGColor.init(gray: 0.0, alpha: 0.0)
Button.setTitleColor(.white, for: .normal)
Button.setTitleColor(.red, for: .highlighted)
Button.layer.borderWidth = 1
Button.layer.cornerRadius = 5
Button.layer.borderColor = UIColor.white.cgColor
Button.isHighlighted = false
Button.showsTouchWhenHighlighted = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: Button)
Button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Do any additional setup after loading the view.
}
}
}
Maybe try what he said but
Just use SwiftUI it is much better
#objc and all of that is fine but in SwiftUI you can just right click and add buttons
There 2 mistakes in the code.
You have to create #objc function for the button action
In your code move viewWillAppear method outside the viewDidLoad
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.init(type: .custom)
button.setTitle("Chat", for: .normal)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.red, for: .highlighted)
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.layer.borderColor = UIColor.white.cgColor
button.isHighlighted = false
button.showsTouchWhenHighlighted = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
button.addTarget(self, action: #selector(didTapRegister), for: .touchUpInside)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
#objc func didTapRegister(_ sender: UIButton) {
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Do any additional setup after loading the view.
}
}
Note: button name start with lowercase.

Make the UIRefreshControl short for swiping

When using the standard UIRefreshControl in TableView the swipe down can be too long, that is, I have to drag my finger almost to the very bottom of the screen.
Is it possible to shorten the swipe path?
var refreshControl = UIRefreshControl()
#objc func refresh(_ sender: AnyObject) {
// Refresh anything
}
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
refreshControl.backgroundColor = UIColor.clear
TableView.addSubview(refreshControl)
refresh(view)
}
Thank you in advance for your help.
Try it.
refreshControl.addTarget(self, action: #selector(refreshHande(_:)), for: .valueChanged)
let ori = tableView.frame
let temp_frame = CGRect.init(x: ori.origin.x, y: ori.origin.y, width:
ori.size.width, height: ori.size.height/1.3 )
tableView.frame = temp_frame
tableView.addSubview(refreshControl)
tableView.frame = ori
TableView has property refreshControl. Use the following code
var refreshControl = UIRefreshControl()
#objc func refresh(_ sender: AnyObject) {
// Refresh anything
}
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
refreshControl.backgroundColor = UIColor.clear
// tableView is a UITableView outlet
tableView.refreshControl = refreshControl
}

Cannot attach existing method to UIButton

I'm attempting to make a mock-up application, but I cannot seem to get the UIButton to hook up to the action that should be executed when I click it.
I'm not sure why it cannot find the method, the names are the same and adding in a (_:) to the #selector does nothing.
import UIKit
class LoginViewController: UIViewController {
var loginButton: UIButton?
#objc func onLoginButtonPress(sender: UIButton!) {
print("Login Button Press")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setup()
}
func setup() -> Void {
initLoginButton()
self.view.backgroundColor = .green
}
func initLoginButton() -> Void {
let loginButtonBounds = CGRect(x: 100, y: 100, width: 250, height: 250)
loginButton = UIButton(frame: loginButtonBounds)
loginButton!.backgroundColor = .red
loginButton!.setTitle("Login", for: .normal)
loginButton!.addTarget(self, action: #selector("onLoginButtonPress"), for: .touchUpInside)
self.view.addSubview(loginButton!)
}
loginButton!.addTarget(self, action: #selector(onLoginButtonPress(sender:)), for: .touchUpInside)
edit this line code, this should work for you. Its better to not use force-unwrapping so avoid using it.
Have you tried removing the parenthesis from "onLoginButtonPress"? I don't think they are needed.
loginButton!.addTarget(self, action: #selector("onLoginButtonPress"), for: .touchUpInside)

Button to reload UIWebView

I have created a button over top of a UIWebView that I would like to use to refresh the webview when pressed. The following code is in my ViewController class:
#IBOutlet weak var webView: UIWebView!
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Landscape
}
override func viewDidLoad() {
super.viewDidLoad()
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
self.view.addSubview(webView)
button.frame = CGRectMake(20, -20, 100, 100)
button.setTitle("Refresh", forState: UIControlState.Normal)
button.addTarget(self, action: Selector(reload()), forControlEvents: UIControlEvents.TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}
I have created the reload method to refresh the webView but nothing happens when I click the button. I also tried to create the reload method inside viewDidLoad to see if this fixed the issue. What could be causing this issue?
action: Selector(reload())
The syntax is wrong. On Xcode version 7.2 and below it should be
action: Selector("reload")
on Xcode 7.3 and up it should be
action: #selector(ClassName.funcName)
In Xcode 7.2 whole thing should be
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
self.view.addSubview(webView)
button.frame = CGRectMake(20, -20, 100, 100)
button.setTitle("Refresh", forState: UIControlState.Normal)
button.addTarget(self, action: Selector("reload"), forControlEvents: .TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}
You defined webview inside view did load scope, take it out:
let webView:UIWebView = UIWebView(frame: CGRectMake(30, 20, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .System)
webView.scalesPageToFit = true
webView.multipleTouchEnabled = true
webView.backgroundColor = UIColor.whiteColor()
webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.example.com")!))
webView.layer.zPosition = 1
self.view.addSubview(webView)
button.frame = CGRectMake(0, 0, 100, 100)
button.setTitle("Reload Page", forState: UIControlState.Normal)
button.addTarget(self, action: Selector(reload()), forControlEvents: UIControlEvents.TouchUpInside)
webView.insertSubview(button, aboveSubview: webView)
}
func reload() {
webView.reload()
}

switch statements in swift using buttons

i cant figure out how to use my switch statement in swift so that when i click either button it will execute the println(). each time i click either button it just executes the default.
class ViewController: UIViewController{
var addOne : UIButton = UIButton(frame: CGRectMake(0, 0, 40, 40))
var addFive : UIButton = UIButton(frame: CGRectMake(0, 0, 40, 40))
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
addOne.setTitle("+1", forState: UIControlState.Normal)
addOne.backgroundColor = UIColor.blackColor()
addOne.center = (CGPointMake(40, 250))
addOne.addTarget(self, action: "player", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(addOne)
addFive.setTitle("+5", forState: UIControlState.Normal)
addFive.backgroundColor = UIColor.blackColor()
addFive.center = (CGPointMake(200, 250))
addFive.addTarget(self, action: "player", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(addFive)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func player(){
switch (true){
case addOne:
println("goodbye")
case addFive:
println("hello world")
default:
println("hello")
}
}
}
Change This:
addOne.addTarget(self, action: "player", forControlEvents: UIControlEvents.TouchUpInside)
To This:
addOne.addTarget(self, action: "player:", forControlEvents: UIControlEvents.TouchUpInside)
And This:
addFive.addTarget(self, action: "player", forControlEvents: UIControlEvents.TouchUpInside)
To This:
addFive.addTarget(self, action: "player:", forControlEvents: UIControlEvents.TouchUpInside)
Note the addition of the colon in "player:"
Next, change This:
func player(){
switch (true){
case addOne:
println("goodbye")
case addFive:
println("hello world")
default:
println("hello")
}
}
To This:
func player(sender: UIButton){
switch (sender){
case addOne:
println("goodbye")
case addFive:
println("hello world")
default:
println("hello")
}
}
Use switch-case statement for applying different states of player with using of UIButton Action.
// --------------------------------------------------------
// MARK:- UIButton Action
// --------------------------------------------------------
#IBAction func onTapPauseBtn(_ sender: Any) {
switch self.player.playbackState {
case .stopped:
self.player.playFromBeginning()
break
case .paused:
self.player.playFromCurrentTime()
btnPause.setImage(UIImage(named: ""), for: .normal)
break
case .playing:
self.player.pause()
btnPause.setImage(UIImage(named: "btn_play_video"), for: .normal)
break
case .failed:
self.player.pause()
break
}
}