当我点击它时,我正在尝试将按钮图标从PLAY更改为PAUSE。但我的代码无效。
这是我的@IBAction代码。
@IBAction func playTimer(sender: AnyObject) {
if buttonSwitch == 0 {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
buttonSwitch = 1
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "setAlarm"), animated: true)
println("2")
} else {
timer.invalidate()
buttonSwitch = 0
}
}
我已经尝试了一切,但没有发生任何事情。没有错误,图标也没有改变。
答案 0 :(得分:2)
//oh sorry! previously i had given Objective c code...,
var imageCheckUI:UIImage = UIImage(named: "checkmark-25.png")!
var rectCheckFram : CGRect = CGRectMake(-10, 0, 20, 20)
var BtnCheckUI : UIButton = UIButton(frame: rectCheckFram)
BtnCheckUI .setBackgroundImage(imageCheckUI, forState: UIControlState.Normal)
BtnCheckUI.addTarget(self, action: "Correctdata", forControlEvents:.TouchUpInside)
imageCheckUI = UIImage(named: "appbar.close.png")!
rectCheckFram = CGRectMake(-10, 0, 30, 30)
var BtnCloseUI : UIButton = UIButton(frame: rectCheckFram)
BtnCloseUI .setBackgroundImage(imageCheckUI, forState: UIControlState.Normal)
BtnCloseUI.addTarget(self, action: "close", forControlEvents:.TouchUpInside)
let barbtncheck: UIBarButtonItem = UIBarButtonItem(customView: BtnCheckUI)
var barbtnRIght :UIBarButtonItem = UIBarButtonItem(customView: BtnCloseUI)
let array:NSArray = NSArray(objects: barbtncheck,barbtnRIght)
self.navigationItem.setRightBarButtonItems(array, animated: true)
答案 1 :(得分:0)
当你说什么都没发生时,我甚至假设你的println都没有显示。在这种情况下,请确保buttonSwitch最初设置为0.如果这没有解决它,请在IBAction函数的第一行上放置一个中断,并在按下按钮时查看它是否被调用。如果没有,您的插座有问题。如果是这样,并且println正在运行,那么您的navigationItem引用就会出现问题。在您的代码中,您正在从播放切换到暂停,但是您需要将反向代码放在else语句中以切换回来(如果这是您想要的)。
答案 2 :(得分:0)
// Use as it iss in you code for swift
UIImage* image3 = [UIImage imageNamed:@"appbar.refresh.png"];
CGRect frameimg = CGRectMake(-10, 0, 30, 30);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(RefresInformation:)forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIImage* image4 = [UIImage imageNamed:@"appbar.save.png"];
CGRect frameimg4 = CGRectMake(0, 0, 30, 30);
UIButton *someButton4 = [[UIButton alloc] initWithFrame:frameimg4];
[someButton4 setBackgroundImage:image4 forState:UIControlStateNormal];
[someButton4 addTarget:self action:@selector(SaveNewProjectRecord:)forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIImage* image5 = [UIImage imageNamed:@"appbar.close.png"];
UIButton *someButton5 = [[UIButton alloc] initWithFrame:frameimg4];
[someButton5 setBackgroundImage:image5 forState:UIControlStateNormal];
[someButton5 addTarget:self action:@selector(CancelTransaction:)forControlEvents:UIControlEventTouchUpInside];
[someButton5 setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
UIBarButtonItem *mailbutton4=[[UIBarButtonItem alloc] initWithCustomView:someButton4];
UIBarButtonItem *mailbutton5=[[UIBarButtonItem alloc] initWithCustomView:someButton5];
self.navigationItem.rightBarButtonItems=[[NSArray alloc] initWithObjects:mailbutton5,mailbutton, mailbutton4, nil];
答案 3 :(得分:0)
这是工作代码,我猜你忘记了":"在行动......也许......
import UIKit
class ViewController: UIViewController {
var buttonSwitch : Int!
var timer : NSTimer!
override func viewDidLoad() {
super.viewDidLoad()
buttonSwitch = 0;
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "playTimer:"), animated: true)
}
@IBAction func playTimer(sender: AnyObject) {
if buttonSwitch == 0 {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("result"), userInfo: nil, repeats: true)
buttonSwitch = 1
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "playTimer:"), animated: true)
println("2")
} else {
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "playTimer:"), animated: true)
timer.invalidate()
buttonSwitch = 0
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 4 :(得分:0)
这是简单的代码。运行Xcode 6.3此代码不会更改按钮标识符。安装程序是单视图控制器。导航栏项目,栏左侧的一个按钮栏项目。将按钮栏项链接到IBAction" playTimer"。
import UIKit
class ViewController: UIViewController {
var buttonSwitch : Int!
var timer : NSTimer!
override func viewDidLoad() {
super.viewDidLoad()
buttonSwitch = 0;
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "playTimer:"), animated: true)
}
@IBAction func playTimer(sender: AnyObject) {
if buttonSwitch == 0 {
println("buttonSwitch = 0")
buttonSwitch = 1
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "playTimer:"), animated: true)
} else {
println("buttonSwitch = 1")
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "playTimer:"), animated: true)
buttonSwitch = 0
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
此论坛上的几个帖子已声称使用此代码或类似代码解决按钮项标识符更改问题,但我和其他尝试此代码的人发现它运行,但按钮标识符不会更改。两个println()语句按预期交替显示,但按钮不会更改。不在viewDidLoad中而不在playTimer中。
请:在主持人删除之前"回答"作为一个答案,请考虑它是上述那些清理的示例答案,并且据称应该起作用。此问题出现在此论坛的至少五个帖子中,但没有一个包含执行的答案。请考虑允许这个"回答"希望聪明的人能够接受这些代码并对其进行修改,以便按钮标识符确实发生变化。