swift – tvOS Button Action

I created a UIButton in tvOS via Swift

let randomBtn = UIButton()
randomBtn. setTitle("Zufällig", forState: .Normal)
let RndNormal = UIImage(named: "RndNormal")
let RndHoover = UIImage(named: "RndHoover")
randomBtn.setImage(RndNormal , forState: .Normal)
randomBtn.setImage(RndHoover, forState: .Focused)
randomBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
randomBtn.setTitleColor(UIColor. whiteColor(), forState: .Focused)

randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside)

let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
randomBtn.frame = CGRect(x: screenWidth-150, y: 60, width: 70, height: 70 )
self.view.addSubview(randomBtn)

But if the button is pressed, the action will never be fired, is there any difference in tvOS?

func click(sender: UIButton) {
print("click")
}

In tvOS for Button action UIControlEvents, TouchUpInside will not be called.

You must use UIControlEvents PrimaryActionTriggered as shown below.

randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)

If you have Any doubts, please refer to this link

https://forums.developer.apple.com/thread/17925

I use Swift in tvOS Created a UIButton

let randomBtn = UIButton()
randomBtn.setTitle("Zufällig", forState: .Normal)
let RndNormal = UIImage(named: "RndNormal")
let RndHoover = UIImage(named: "RndHoover")
randomBtn.setImage(RndNormal, forState: .Normal)
randomBtn.setImage(RndHoover, forState : .Focused)
randomBtn.setTitleColor(UIColor.blackColor(), forState: .Normal)
randomBtn.setTitleColor(UIColor.whiteColor(), forState: .Focused)

randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents.TouchUpInside)

let screenSize: CGRect = UIScreen.mainScreen(). bounds
let screenWidth = screenSize.width
randomBtn.frame = CGRect(x: screenWidth-150, y: 60, width: 70, height: 70)
self.view.addSubview(randomBtn )

But if the button is pressed, the action will never be fired, is there any difference in tvOS?

func click(sender: UIButton) {
print("click")
}

In tvOS for Button action UIControlEvents, TouchUpInside will not be called.

You must use UIControlEvents PrimaryActionTriggered as shown below.

< pre>randomBtn.addTarget(self, action: #selector(ViewController.click(_:)), forControlEvents: UIControlEvents. PrimaryActionTriggered)

If you have any doubts, please refer to this link

< p>https://forums.developer.apple.com/thread/17925

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4103 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.