首页 技术 正文
技术 2022年11月15日
0 收藏 963 点赞 4,918 浏览 1613 个字

接着下来简单说说Label(相当于android的textview)和button的使用, 由于都是与上篇的AppDelegate一致, 所以这一篇就说说ViewController与xib的使用呗。

BIDViewController.h

#import <UIKit/UIKit.h>@interface BIDViewController : UIViewController    // 类的开始@property (weak, nonatomic) IBOutlet UILabel *statusLabel;    
// a. @property是定义属性的关键字;
b. weak与strong关键字的区别, strong表示对象没有被释放则一直持有对象, 而weak指向的地址一旦被释放,这些指针都将被赋值为nil
c. atomic、nonatomic、assign、copy、retain关键字的区别, atomic是默认的设置,提供多线程安全, 但是会影响效率; nonatomic线程不安全, 提高性能; assign用于基本的数据类型; retain用于NSObject和其子类; copy复制对象到新的地址;
ps:copy 其实是建立了一个相同的对象,而 retain 不是:比如一个NSString 对象,地址为 ×,内容为 @”STR” , copy 到另外一个 NSString 之后,地址为 × ,内容相同,新的对象 retain 为 ,旧有对象没有变化; retain 到另外一个 NSString 之后,地址相同(建立一个指针,指针拷贝),内容当然相同,这个对象的 retain 值 +。也就是说, retain 是指针拷贝, copy 是内容拷贝。
IBOutlet只是一个标记, 用于表示已在xib定义实现, xib连接时按住ctrl键, 鼠标从File's Owner拖动到在代码中标有IBOutlet的空间上, 然后在弹出框选择- (IBAction)buttonPressed:(UIButton *)sender;    // 定义操作触发的函数, IBAction也是一个标识, 标记这是触发函数, xib中在需要触发函数的控键上按住ctrl键, 鼠标从该控键拖动到File's Owner并选择相应的函数即可@end    // 类的结束

BIDViewController.m

#import "BIDViewController.h"@implementation BIDViewController
@synthesize statusLabel; // 与property一般成对出现- (IBAction)buttonPressed:(UIButton *)sender {
NSString *title = [sender titleForState:UIControlStateNormal]; // 返回控键指定状态的title
NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title]; // %@表示这个参数时对象类型 /*** 让部分文字变色方法 **/
NSMutableAttributedString *styledText = [[NSMutableAttributedString alloc]
initWithString:plainText];
// 这语法有点儿像json哇
NSDictionary *attributes = @{
NSFontAttributeName : [UIFont boldSystemFontOfSize:statusLabel.font.pointSize]
}; NSRange nameRange = [plainText rangeOfString:title]; [styledText setAttributes:attributes
range:nameRange];
statusLabel.attributedText = styledText;
}
@end

第二篇结束!!!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294