我有一个问题,我想放一个按钮而不是@“/ n”即,@“uibutton”
BibleIphoneAppDelegate *appDelegate = (BibleIphoneAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *allVerses = [appDelegate.biblearray valueForKey:@"verseNumber"];
_bibleDesciption.text = [allVerses componentsJoinedByString:@"/n"];
verseNumber包含圣经的经文,我需要在经文之间加一个按钮。我希望你理解我的问题。帮助我做这个。 提前谢谢。
答案 0 :(得分:0)
我没有对此进行测试,因此可能包含一些错误,但应该可以帮助您入门。
// set these as members of your class
NSMutableArray *_versesLabels; // used for easier access when a button is pressed
NSMutableArray *_versesButtons;
/*IBOutlet */UIScrollView *_scrollView;
// your initialization/loading method
// <..set up _scrollView as you wish>
int labelWidth = 300; // change this as you wish
int buttonWidth = 100, buttonHeight=30;
int padding = 5; // space between labels/buttons from the scrollView
BibleIphoneAppDelegate *appDelegate = (BibleIphoneAppDelegate *)[[UIApplication sharedApplication] delegate];
NSArray *allVerses = [appDelegate.biblearray valueForKey:@"verseNumber"];
int currentY = padding;
for(int i=0; i<[allVerses count]; i++){
// create and place the label
UILabel *lbl = [[UILabel alloc] init];
lbl.text = [allVerses objectAtIndex:i];
// <..customize the label - fonts, colors etc>
lbl.numberOfLines = 0; // in case you have verses that don't fit in a single line
CGSize size = [lbl.text sizeWithFont:lbl.font constrainedToSize:CGSizeMake(labelWidth, 500)];
lbl.frame = CGRectMake(0, currentY, labelWidth, size.height);
[_scrollView addSubView:lbl];
[_versesLabels addObject:lbl];
[lbl release];
currentY=currentY + lbl.frame.size.height + padding;
// create and place the button
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, currentY, buttonWidth, buttonHeight)];
btn.tag = i;
// <..customize the button - fonts, colors etc>
[btn addTarget:self action:@selector(pressVerse:) event:UIEventTouchUpInside];
[_scrollView addSubView:btn];
[_versesButtons addObject:btn];
[btn release];
currentY=currentY + btn.frame.size.height + padding;
}
[_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width, currentY)];
-(void)pressVerse:(UIButton *)b{
verseIndex = b.tag;
UILabel *verseLabel = [_verseLabels objectAtIndex:verseIndex];
// <..do whatever you want when a button is pressed>
}