UIBarButtonItem:

一般 UIBarButtonItem 會放在上圖所示的 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 ,會彈出下圖所示提示框:

它的顯示代碼就是 源于 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
// 如果沒有設(shè)置參數(shù) cancelButtonTitle: 那么將處于模態(tài)窗口無法退出,這時,使用這行代碼,效果就是一點(diǎn)擊alert,彈出窗口后由會立刻關(guān)閉。
}
在看完上面這段代碼,我們需要知道如何在模態(tài)窗口點(diǎn)擊 one 或者 two 之后觸發(fā)事件。所以,這樣就需要在方法中設(shè)置 delegate: 。同時需要在實(shí)現(xiàn)文件 .m 頂部申明相關(guān)的協(xié)議:
@interface BLOneViewController ()<UIAlertViewDelegate, UIActionSheetDelegate>
@end
分三步,第一步是上圖,第二步是 delegate:self, 第三步真正實(shí)現(xiàn)代理方法(在例子中實(shí)現(xiàn)了兩個方法):
#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];
}
}
}
上面兩個方法中第一個參數(shù)聲明是哪一個的代理,第二個參數(shù)聲明的是對應(yīng)哪一個按鍵。上面兩個方法的作用時一樣的,只是完成的時間節(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; //這樣表示對行數(shù)沒有限制
label.lineBreakMode = NSLineBreakByWordWrapping; //表示按單詞進(jìn)行換行,也有按字符進(jìn)行換行的,一般使用前者。
由另一種情況,當(dāng)我們輸入當(dāng)label中的內(nèi)容很多事,如何能讓系統(tǒng)自動地調(diào)整 UILabel 的frame 來適應(yīng)文本內(nèi)容的增加呢?看如下的代碼:
CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width - 25, CGFLOAT_MAX) // 即label.text 這段文本最高,最寬都不超過這里面設(shè)置的內(nèi)容
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin // option 是指里面的字體如何設(shè)置,一般將這兩個參數(shù)輸入即可(可以再去詳細(xì)了解)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16], } // 這里面設(shè)置的事它的字體大小屬性(注意字體條件和前文設(shè)置的屬性要保持一致。)
context:nil].size;
label.frame = CGRectMake(10, label.frame.origin.y, textSize.width, textSize.height);