ios基础之入门(一)

时间:2024-01-14 12:48:56

最近找到了一个可以接触ios开发的职位,可以系统的学习和练习了。先从最基本的开始:

一、获取控件的两种方式

1)第一种,也是经常使用的一种,通过IBOutlet方式。直接按住control键,将控件和ViewController建立联系,然后就可以通过Controller的属性来获取控件

2)第二种,通过设置控件的tag属性。

先设置控件的tag属性为一个integer类型的值,然后在代码中获取

 UILabel *lbl2 = (UILabel*)[self.view viewWithTag:];

二、事件处理的三种方式

1)第一种,最常使用的是通过IBAction的方式

2)第二种,通过代码设置事件处理方法

在viewDidLoad事件中调用addTarget:action:forControlEvents来添加处理事件,

addTarget:表明以改对象的某个方法来处理事件,例如当前controller

action:代表处理事件的方法

forControlEvents:要处理的事件类型,一个UIControlEvents的枚举值

例子:

- (void) viewDidLoad
{
[super viewDidLoad];
NSLog(@"页面加载完成");
AppDelegate *ad = [UIApplication sharedApplication].delegate;
NSLog(@"打印代理类的lycname属性%@", ad.LycName); //给控件添加事件
[self.btnMeet addTarget:self action:@selector(btnMeet_click:) forControlEvents:UIControlEventTouchUpInside];
} -(void) btnMeet_click:(UIButton *) sender
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"我是遇见按钮" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];

3)通过委托对象来处理事件

上两种事件处理针对的都是主动控件,即继承自UIControl:UIView的控件。

而如果是UITextView这种控件,是继承自UIScrollView:UIView,所以它本身并不具备一些常见事件,在这种情况下,就需要委托对象来处理特殊事件。

例如刚刚提到的UITextView控件,它的默认事件有:

-textViewShouldBeginEditing:将要开始编辑时触发

-textViewDidBeginEditing:开始编辑后触发

-textViewShouldEndEditing:将要结束编辑时触发

-textViewDidEndEditing:结束编辑后触发

-textViewDidChange:文本内容发生改变后触发

要实现上面的事件处理,必须要实现UITextViewDelegate协议

 @interface UITextFieldViewController : UIViewController<UITextViewDelegate>
//属性
@property (strong,nonatomic) UIBarButtonItem *doneRightBI;//右侧导航条按钮-完成
@property (strong,nonatomic) UIBarButtonItem *commitRightBI;//右侧导航条按钮-提交 @end
@implementation UITextFieldViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.txtContent.delegate = self; if (self.navigationItem != nil) {
[self.navigationItem setTitle:@"文本框演示"]; //初始化右侧导航按钮-提交按钮
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(registerUserHandler)]; self.commitRightBI = rightBtn;
//设置当前右侧按钮为提交按钮
[self.navigationItem setRightBarButtonItem:self.commitRightBI]; //初始化右侧导航按钮-完成按钮
UIBarButtonItem *rightDoneBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignFirstReponderHandler)];
self.doneRightBI = rightDoneBtn; } }
//上面的按钮处理事件(registerUserHandler、resignFirstReponderHandler)就不列出来了
@end
 //文本框开始编辑时触发
-(void) textViewDidBeginEditing:(UITextView *)textView
{
NSLog(@"开始编辑本文域");
if (self.navigationItem != nil)
{
[self.navigationItem setRightBarButtonItem:self.doneRightBI animated:NO];
}
} //文本框结束编辑时触发
-(void) textViewDidEndEditing:(UITextView *)textView
{
NSLog(@"结束编辑本文域");
[self resignFirstReponderHandler];
if (self.navigationItem != nil)
{
[self.navigationItem setRightBarButtonItem:self.commitRightBI animated:NO];
}
}

这样,通过上面的代码,当UITextView在不同状态时,右侧的导航按钮也会相应的改变

三、通过代码创建控件

1、创建UI控件

2、调用addSubView将创建的控件添加到其他view中

3、多次调用控件的setter方法,设置外观、行为等

例子:

    //用代码创建一个控件
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置button的大小
button.frame=CGRectMake(, , , );
[button setTitle:@"我是代码创建的" forState:UIControlStateNormal];
[self.view addSubview:button];