我有这个代码,单击按钮时,它的数字加1或减1。 我怎么能改变它,所以它会写例子xxxx而不是4?
我尝试了一个while循环,但似乎无法让它工作。
-(void)Changefouls:(id)sender{
if (e == 5) {
e = 5;
self.homefouls.text = [NSString stringWithFormat:@"%i", e];
} else {
e = e + 1;
self.homefouls.text = [NSString stringWithFormat:@"%i", e];
}
}
-(void)Changefouls2:(id)sender {
if (e < 1) {
e = 0;
self.homefouls.text = [NSString stringWithFormat:@"%i", e];
} else {
e = e - 1;
self.homefouls.text = [NSString stringWithFormat:@"%i", e];
}
}
答案 0 :(得分:0)
好的,首先让你的生活更轻松,并使用有意义的名字。
将e
更改为_foulCount
或_numberOfFouls
或其他内容(我假设您已在@interface
中将其声明为实例变量或属性)。
接下来将操作方法更改为foulsPlusPressed:
和foulsMinusPressed:
。
然后:
-(void)foulsPlusPressed:(id)sender {
if (_foulCount == 5)
return; // Nothing more to do
_foulCount++;
[self _updateFoulsText]; // private method
}
-(void)foulsMinusPressed:(id)sender {
if (_foulCount == 0)
return; // Nothing more to do
_foulCount--;
[self _updateFoulsText]; // private method
}
// Private method
- (void)_updateFoulsText {
NSMutableString *str = [[NSMutableString alloc] init];
for (unsigned i = 0; i < _foulCount; i++)
[str appendString:@"x"];
self.homefouls.text = str;
}
答案 1 :(得分:0)
这是一个很好的替代解决方案,没有使用NSString类中的stringByPaddingToLength方法进行循环。此方法通过从末尾删除字符或通过附加给定填充字符串所需的多次出现来返回从接收器形成的新字符串。
代码示例:
@interface ViewController ()
@property (strong, nonatomic) UITextField *textField;
@end
@implementation ViewController
- (void)viewDidLoad {
// basic UI for demo
_textField = [[UITextField alloc] initWithFrame:CGRectMake(0,
25,
CGRectGetWidth(self.view.frame),
25)];
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.placeholder = @"text will be replaced with x's";
[self.view addSubview:_textField];
UIButton *btnPlus = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnPlus.frame = CGRectMake(CGRectGetMidX(self.view.frame), 100, 25, 25);
[btnPlus addTarget:self action:@selector(setPlus) forControlEvents:UIControlEventTouchUpInside];
[btnPlus setTitle:@"+" forState:UIControlStateNormal];
[self.view addSubview:btnPlus];
UIButton *btnMinus = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnMinus.frame = CGRectMake(CGRectGetMidX(self.view.frame), 130, 25, 25);
[btnMinus addTarget:self action:@selector(setMinus) forControlEvents:UIControlEventTouchUpInside];
[btnMinus setTitle:@"-" forState:UIControlStateNormal];
[self.view addSubview:btnMinus];
}
- (void)setPlus {
/*
takes the current length for textField and adds one at the beginning
*/
_textField.text = [_textField.text stringByPaddingToLength:_textField.text.length + 1
withString:@"x"
startingAtIndex:0];
NSLog(@"adding: %lu", _textField.text.length);
}
- (void)setMinus {
if (_textField.text.length > 0) { // checks if there anything to remove, avoids out of memory crash
_textField.text = [_textField.text stringByPaddingToLength:_textField.text.length - 1
withString:@"x"
startingAtIndex:0];
NSLog(@"removing: %lu", _textField.text.length);
}
}
@end
答案 2 :(得分:-1)
试试:
NSMutableString *str = [[NSMutableString alloc] init];
for (int i = 0; i< YOURNUMBER; i++)
{
[str appendString:@"x"];
}
NSLog(@"%@ str");
这用于循环,它将枚举YOURNUMBER值的次数,并将x字符串添加到str对象。 NSLog行只是将结果打印到控制台。您可能希望将其替换为:
self.homefouls.text = str;