问题描述
更新1
我通过将条件放在 dealCard
中而不是在 startPause
中解决了循环不停止的问题.
I fixed the problem of the loop not stopping by putting the condition in dealCard
instead of in startPause
.
更新1
更新0
- (IBAction)startPause: (UIButton *)sender{
NSString *buttonTitle = sender.currentTitle;
if ([buttonTitle isEqualToString: @"Pause"]) {
[self.myButton setTitle:@"Resume" forState:UIControlStateNormal];
[timer invalidate];
}
else
{
[self.myButton setTitle:@"Pause" forState:UIControlStateNormal];
if ([self.deal length]>cardNum) {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(dealCard)
userInfo:nil
repeats:YES];
}
else
[timer invalidate];
}
}
除了上述修改后的代码外,我还为多个人建议的 myButton
添加了 IBOutlet
属性,并将 NSTimer
更改为 repeats:NO
中的 repeats:YES
,我从 dealCard
中删除了呼叫 [self startPause]
.现在,该系统似乎可以正常工作,只是"while"条件的失败 self.deal length]> cardNum
不能正确使 else中的
子句. NSTimer
失效
In addition to the revised code above, I added a IBOutlet
property for myButton
as suggested by multiple folks, and changed the NSTimer
to repeats:YES
from repeats:NO
, and I removed the call [self startPause]
from dealCard
. Now the system seems to work except that the failure of the "while" condition self.deal length]>cardNum
does not properly invalidate the NSTimer
in the else
clause.
我可以使用一些有关为何无效者未被解雇的反馈.
I could use some feedback about why that invalidate is not getting fired.
更新0
继续回答这个问题,我正在尝试添加"startPause" UIButton的暂停"部分.点击按钮后,我希望其标题和操作为暂停",然后希望其标题在恢复"和暂停"之间切换.
Continuing this answered question, I am trying to add the "Pause" part of a "startPause" UIButton. After the button is tapped, I want its title and action to be "Pause", and I want its title to toggle then between "Resume" and "Pause".
该按钮是在情节提要中创建的,我无法考虑如何为该按钮创建标识符,因此可以通过编程方式对其进行更改.我怀疑我现在需要增强按钮的 IBAction
方法以包括发送者",但我可能还遥遥无期.
The button is created in Storyboard and I cannot think of how to create an identifier for the button so I can alter it programatically. I suspect I need to enhance the button's IBAction
method to include "sender" now, but I may be way off.
当前,我有以下未完成的代码.
Currently I have the following unfinished code.
- (IBAction)startPause: (UIButton *)sender{
NSString *buttonTitle = sender.currentTitle;
if ([buttonTitle isEqual: @"Pause"]) {
[myButton setTitle:@"Resume" forState:UIControlStateNormal]; //**** myButton is what?
if ([self.deal length]>cardNum) {
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(dealCard)
userInfo:nil
repeats:NO];
}
}
}
(我知道我还没有开始代码的"else"部分.)
(I know I have not started the "else" part of the code.)
如何使用情节提要中的 startPause
按钮将上面的 myButton
连接起来?
How do I hook up myButton
above with my startPause
button in the Storyboard?
推荐答案
听起来好像您在Storyboard中有一些错误.对于UIButton(myButton),您应该在.h文件中有两个钩子
It sounds like you have a few things wired up wrong in Storyboard. For your UIButton (myButton) you should have two hooks into your .h file
(1) @property (strong, nonatomic) IBOutlet UIButton *myButton;
(2) - (IBAction)startPause:(id)sender;
(1)允许您在.m文件中的任何位置设置按钮标题,尤其是在myButtonAction事件中.(2)当您将按钮连接到.h文件时,您应该选择touchUpInside,这样,当点击按钮时,事件myButtonAction将触发.
(1) will allow you to set your button title anywhere within your .m file, specifically in your myButtonAction event.(2) when you wired the button to your .h file you should have chosen touchUpInside, that way when the button is tapped the event myButtonAction will fire.
EDIT2(用于寻址NSTimer)
EDIT2 (to address NSTimer)
- (IBAction)startPause: (UIButton *)sender{
NSString *buttonTitle = sender.currentTitle;
if ([buttonTitle isEqualToString: @"Pause"]) {
[self.myButton setTitle:@"Resume" forState:UIControlStateNormal];
[timer invalidate];
}
else
{
[self.myButton setTitle:@"Pause" forState:UIControlStateNormal];
if ([self.deal length]>cardNum)
{
if (!myTimer.isValid)
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(dealCard)
userInfo:nil
repeats:YES];
}
else
{
//do nothing - timer has been created already
}
}
else
{
[timer invalidate];
}
}
}
此文档可能也有帮助.
这篇关于识别一个故事板uibutton以便在代码中进行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!