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 。
当我们点击 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: 也设置了nil
// 如果没有设置参数 cancelButtonTitle: 那么将处于模态窗口无法退出,这时,使用这行代码,效果就是一点击alert,弹出窗口后由会立刻关闭。
}
在看完上面这段代码,我们需要知道如何在模态窗口点击 one 或者 two 之后触发事件。所以,这样就需要在方法中设置 delegate: 。同时需要在实现文件 .m 顶部申明相关的协议:
@interface BLOneViewController ()<UIAlertViewDelegate, UIActionSheetDelegate>
@end
分三步,第一步是上图,第二步是 delegate:self, 第三步真正实现代理方法(在例子中实现了两个方法):
#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];
}
}
}
上面两个方法中第一个参数声明是哪一个的代理,第二个参数声明的是对应哪一个按键。上面两个方法的作用时一样的,只是完成的时间节点不一样。
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; //这样表示对行数没有限制
label.lineBreakMode = NSLineBreakByWordWrapping; //表示按单词进行换行,也有按字符进行换行的,一般使用前者。
由另一种情况,当我们输入当label中的内容很多事,如何能让系统自动地调整 UILabel 的frame 来适应文本内容的增加呢?看如下的代码:
CGSize textSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width - 25, CGFLOAT_MAX) // 即label.text 这段文本最高,最宽都不超过这里面设置的内容
options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin // option 是指里面的字体如何设置,一般将这两个参数输入即可(可以再去详细了解)
attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16], } // 这里面设置的事它的字体大小属性(注意字体条件和前文设置的属性要保持一致。)
context:nil].size;
label.frame = CGRectMake(10, label.frame.origin.y, textSize.width, textSize.height);