UIBarButtonItem:
一般 UIBarButtonItem 會(huì)放在上圖所示的 navigationBar 上面(放在navigationBar和tabBar上的都屬于UIBarButtonItem)。上圖右邊的alert 顯示 头遭,代碼如下(注意添加 alertButton 的方式):
UIBarButtonItem *alertButton = [[UIBarButtonItem alloc] initWithTitle:@"alert"
style:UIBarButtonItemStylePlain
target:self
action:@selector(alertButtonClicked:)];
self.navigationItem.rightBarButtonItem = alertButton;
包括上圖所示的 tabBatItem 也是屬于 UIBarButtonItem 寓免。
當(dāng)我們點(diǎn)擊 navigationBar 右邊的 alert ,會(huì)彈出下圖所示提示框:
它的顯示代碼就是 源于 action:@selector(alertButtonClicked:), 它的方法代碼段如下所示:
- (void)alertButtonClicked:(UIBarButtonItem *)button
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title"
message:@"alert message"
delegate:self
cancelButtonTitle:@"cancel"
otherButtonTitle:@"one", @"two", nil];
alert.tag = kOneAlertViewTag;
[alert show];
// [alert dismissWithClickedButtonIndex:0 animated:YES]; 如果cancelButtonTitle: 也設(shè)置了nil
// 如果沒(méi)有設(shè)置參數(shù) cancelButtonTitle: 那么將處于模態(tài)窗口無(wú)法退出计维,這時(shí)袜香,使用這行代碼,效果就是一點(diǎn)擊alert鲫惶,彈出窗口后由會(huì)立刻關(guān)閉蜈首。
}
在看完上面這段代碼,我們需要知道如何在模態(tài)窗口點(diǎn)擊 one 或者 two 之后觸發(fā)事件。所以欢策,這樣就需要在方法中設(shè)置 delegate: 澜术。同時(shí)需要在實(shí)現(xiàn)文件 .m 頂部申明相關(guān)的協(xié)議:
@interface BLOneViewController ()<UIAlertViewDelegate, UIActionSheetDelegate>
@end
分三步,第一步是上圖猬腰,第二步是 delegate:self, 第三步真正實(shí)現(xiàn)代理方法(在例子中實(shí)現(xiàn)了兩個(gè)方法):
#pragma mark - UIAlertViewDelegate methods
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == kOneAlertViewTag) {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"one"]) {
UIActionSheet *actionSheet
= [[UIActionSheet alloc] initWithTitle:@"title"
delegate:self
cancelButtonTitle:@"cancel"
destructiveButtonTitle:@"destructive"
otherButtonTitles:@"one", @"two", nil];
[actionSheet showInView:self.view];
}
}
}
上面兩個(gè)方法中第一個(gè)參數(shù)聲明是哪一個(gè)的代理鸟废,第二個(gè)參數(shù)聲明的是對(duì)應(yīng)哪一個(gè)按鍵。上面兩個(gè)方法的作用時(shí)一樣的姑荷,只是完成的時(shí)間節(jié)點(diǎn)不一樣盒延。
UIImageView:
UIImage *imageView = [[UIImage alloc] initWithFrame:CGRectMake(10, presentButton.frame.origin.y + presentButton.frame.size.height + 20, self.view.frame.size.width - 20, 100)];
imageView.image = [UIImage imageNamed:@"bg5.png"];
imageView.backgroundColor = [UIColor whiteColor];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
UILabel: 用于顯示文本
里面注意這些方法使用:
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 0; //這樣表示對(duì)行數(shù)沒(méi)有限制
label.lineBreakMode = NSLineBreakByWordWrapping; //表示按單詞進(jìn)行換行,也有按字符進(jìn)行換行的鼠冕,一般使用前者添寺。
由另一種情況,當(dāng)我們輸入當(dāng)label中的內(nèi)容很多事懈费,如何能讓系統(tǒng)自動(dòng)地調(diào)整 UILabel 的frame 來(lái)適應(yīng)文本內(nèi)容的增加呢计露?看如下的代碼:
CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width - 25, CGFLOAT_MAX) // 即label.text 這段文本最高,最寬都不超過(guò)這里面設(shè)置的內(nèi)容
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin // option 是指里面的字體如何設(shè)置憎乙,一般將這兩個(gè)參數(shù)輸入即可(可以再去詳細(xì)了解)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16], } // 這里面設(shè)置的事它的字體大小屬性(注意字體條件和前文設(shè)置的屬性要保持一致票罐。)
context:nil].size;
label.frame = CGRectMake(10, label.frame.origin.y, textSize.width, textSize.height);