更改按钮标签
我是iOS应用开发新手。目前我正在研究一个非常简单的应用程序,它在屏幕上显示两个单词,用户选择一个,应用程序只输出已选择的x。更改按钮标签
我已经创建了两个plists,我已经将它们加载到一个数组并将它们随机化到数组中。在我看来,我有两个按钮,我想单词显示在他们身上。但是,我有两个问题.. 1。我希望在应用程序启动时更改按钮上的标签。所以用户不必点击按钮或任何东西。 2.我想从array_1中的一个随机单词(从plist加载)出现在随机按钮上。 button_1或者button_2,对列表array_2也是如此。
这是我迄今为止所做的。 (来自论坛(一些帮助:)
- (IBAction)buttonpressed:(id)sender {
// Load contents of plist onto array
NSString *path = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
NSMutableArray *words = [NSMutableArray arrayWithContentsOfFile:path];
//shuffle them using Fisher–Yates shuffle algorithm
for (int i = words.count-1; i>=0; i--) {
int r = arc4random_uniform(words.count);
[words exchangeObjectAtIndex:i withObjectAtIndex:r];
}
// assign word to the button
for (int j =0; j<_WordButton.count; j++) {
UIButton *button = [_WordButton objectAtIndex:j];
button.titleLabel.text = [words objectAtIndex:j];
}
}
显然有在上面的代码中的缺陷,例如它仅使用一个plist中,并没有对两个按钮显示文字随机
做这样的:
-(void)viewDidLoad{
[self setnewtitle];
}
-(void)setnewtitle{
NSString *pathone = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
NSMutableArray *wordsOne = [NSMutableArray arrayWithContentsOfFile:pathone];
NSString *pathtwo = [[NSBundle mainBundle] pathForResource:@"wordsOne" ofType:@"plist"];
NSMutableArray *wordsTwo = [NSMutableArray arrayWithContentsOfFile:pathtwo];
int words = "the number of objects(words) in your plist";
int randombutton = arc4random() % 2;
int randomplist = arc4random() % 2;
int randomWord = arc4random() % words;
if (randombutton==0){
if (randomplist==0){
[[_WordButton objectatIndex:randombutton] setTitle:[wordsOne objectatIndex:randomWord]];
} else {
[[_WordButton objectatIndex:randombutton] setTitle:[wordsTwo objectatIndex:randomWord]];
}
}else {
if (randomplist==0){
[[_WordButton objectatIndex:randombutton] setTitle:[wordsOne objectatIndex:randomWord]];
} else {
[[_WordButton objectatIndex:randombutton] setTitle:[wordsTwo objectatIndex:randomWord]];
}
}
}
普莱斯e确保包含按钮的阵列已初始化并分配,如果您有任何问题,请随时询问
抱歉的男人,没有工作,没有反应..我创建了一个名为buttonPressed的方法,返回IBAction并从它调用你的方法,, 我也试过5个字..没有.. – user2301717 2013-04-20 10:40:46
对不起,然后我从手机做到了这一点,我在我的一个应用程序中使用过这种方式,它起作用,您是否分配并初始化了按钮? – Maurice 2013-04-20 10:41:33
我宣布它.. @property(strong,nonatomic)IBOutletCollection(UIButton)NSArray * WordButton; – user2301717 2013-04-20 10:44:39
可以。设置UIButton的标题用下面的代码:
[button setTitle:titleString forState:UIControlStateNormal];
谢谢,你的回答是真的有帮助 – user2301717 2013-04-20 17:11:21
要实现您所期望的操作,您必须创建一个IBOutlet到您的按钮上.xib文件。
- 在Xcode开拓的.xib文件,该文件会显示一个:你可以用一个简单的Xcode的快捷方式,将同时创建IBOutlet中的代码,并从的.xib文件中的代码连接为你做这个图形文件使用Interface Builder。
在Xcode右上角的工具栏中有一个图标,允许您切换不同的模式,选择'Assistant Editor'。接口生成器将滑过共享一个代码文件的窗口,这应该会自动选择ViewController.h
工具栏应该是这样的:插入一些花括号中的代码一样所以:
`
@interface CustomViewController : UIViewController {
// This is where you will control-drag to from the Interface Builder to have it create an IBOutlet to the button for you
}
// Your already set-up IBAction Method
- (IBAction)buttonpressed:(id)sender
@end
`
从你希望你的标签在.h文件中的大括号之间改变,该区域的按钮控制拖拽:
新对话框将会出现。确保设置如下:
a。连接= IBOutlet
b。姓名=(无论你想调用按钮,你已经使用'_wordButton')
c。类型= UIButton
d。存储=弱- 现在,在动作方法
- (IBAction)buttonpressed:(id)sender
和在应用程序中:didFinishLaunchingWithOptions(自动字加载到标签时,应用程序是推出),您可以设置使用以下按钮标题:
`
// Create a string from the word that you pull out of the plist file
NSString *labelWord = ???;
// Set the label of the button
[_wordLabel setTitle:word forState:UIControlStateNormal];
`
谢谢你,你的回答很有帮助。 :) – user2301717 2013-04-21 02:49:43
你确定_WordButton数组只包含按钮吗? – 2013-04-20 09:18:38
是的,我的视图上只有两个按钮。 @property(strong,nonatomic)IBOutletCollection(UIButton)NSArray * WordButton; 和我做出这两个按钮的出口收集连接到这个阵列。 – user2301717 2013-04-20 09:50:01
男人,你在混合两件事 - 试图找出问题的原因:是在你的plist还是在按钮标签中。简化和检查。 – 2014-03-30 14:14:15