所以我之前問過這個問題,並得到了一些答覆,但事情變得非常混亂,所以我決定在這裏重新提問。通過在數組中循環創建按鈕
我在這裏初始化滾動視圖:
ScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
ScrollView.showsVerticalScrollIndicator=YES;
ScrollView.scrollEnabled=YES;
ScrollView.bounces = YES;
ScrollView.userInteractionEnabled=YES;
[self.view addSubview:ScrollView];
ScrollView.contentSize = CGSizeMake(10500,480);
[self.view addSubview:homeButton];
我必須要創造了44個按鍵陣列,陣列包含有關名稱的信息,對畫面的文件名,座標等
我正在此:
for (int i = 0; i < 44; i++) {
[self makeLabelsAndButtons:i];
xPresCoord += 10;
}
在我的viewDidLoad
這是方法makeLabelsAndButtons:
-(void)makeLabelsAndButtons:(NSInteger)indexPath{
Button = [UIButton buttonWithType:UIButtonTypeCustom];
[Button addTarget:self action:@selector(presPressed:) forControlEvents:UIControlEventTouchUpInside];
Button.frame = CGRectMake(160.0, 240.0, 220.0, [[numbers objectAtIndex:indexPath] integerValue]);
Button.center = CGPointMake(xPresCoord, 200.0);
[Button setBackgroundImage:[UIImage imageNamed:[picArray objectAtIndex:indexPath]] forState: UIControlStateNormal];
[self.ScrollView addSubview:Button];
Label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 32)];
Label.text = [nameArray objectAtIndex:indexPath];
Label.center = CGPointMake([[numbers objectAtIndex:indexPath] integerValue], 390);
[Label setFont:[UIFont fontWithName:@"GurmukhiMN" size:27]];
Label.numberOfLines = 1;
Label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5f];
[Label sizeToFit];
Label.textColor= [UIColor whiteColor];
[ScrollView addSubview:Label];
//[ScrollView insertSubview:Label atIndex:1];
//NSLog(@"name: %@, pic:%@, picY:%i", nameString, picString, picNumb);
for (UIView *subview in ScrollView.subviews) NSLog(@"View: %@", subview);
}
最後的NSLog顯示未添加子視圖(標籤和按鈕),但我不知道爲什麼。
UIButton *Button;
UILabel *Label;
在我的.h文件
任何幫助表示讚賞:爲
按鈕和標籤的定義!
編輯:這裏是鏈接到其他問題
Creating buttons with an Array
我想你的代碼的一天,這對我的工作確定。 ScrollView不是零嗎?你怎麼聲明Button?他們是屬性還是ivars? – rdelmar 2013-04-21 23:38:06
ScrollView不是零,ScrollView既是一個屬性,也是我在頭文件中聲明的UIScrollView,我知道它可行,因爲我在界面生成器中有一些項目,它們在那裏,所以我知道視圖存在。按鈕定義爲UIButton * Button;在我的標題 – tyler53 2013-04-21 23:42:34
中可以看到您使用的代碼的.h和.m文件嗎?看看有什麼不同? – tyler53 2013-04-21 23:43:14