从零开始:iOS(一)

前言:开博写这个系列,记录自己学习ios开发的过程.

我是以<objective-c 2.0程序设计>作为入门书籍开始的.

书中第一个例子是一个command line utility程序.(c#:console app)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSLog (@"Hello World");
    [pool drain];
    return 0;
}

xcode提示有错,NSAutoreleasePool 不可用.经过搜索得知Xcode4.2之后的版本中引入了ARC特性来自动管理内存.将代码修改如下.

(暂时不管 ARC是什么,书读到后面,没准就知道了,先把程序走过.)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSLog(@"Hello World");
    }
    return 0;  
}

控制台成功输入:Hello World. 拿c#的开发经验看,这个#import与命名空间类似,NSLog与Console.Write类似,@autoreleasepool就先假想成using指令吧,这些都先不管.一个控制台程序,不太好看,起码得有个视图吧.于是New一个Project,选择Single View Applcation.

输入项目名称:HelloWorld,下一步完成创建. xcode左侧是展开的文件里,有ViewController_iPhone.xib,选中会出现一个iPhone视图.右下角,可以看到控件,鼠标停留有智能显示.拖动一个Lable控件到面板. 双击输入文字 "Hello World".选择iPhone模拟器,Run!

可以在模拟器里看到Hello World.到这里还是不满足,既然有文本框,按钮,那应该可以再多做点事.

将textbox,button拖入到视图,如图.

我想做的是,在文本框输入名字,点击按钮,更新 Hello为{you name},Hello.

如图,选中右上角,Editor选项中,中间那个.工作区会变成一边面板,一边代码.

接下来,选中文本框点击右键 选中Referencing Outlets,拖动到 ViewController.h文件里.操作如下.

(百度老师给出的信息是:Outlets里面显示的是你的属性, 以及连接着的目标.Referencing Outlets是你被连接到了别人的属性上面,先不管,就当是给这个控件赋了id值)

.h文件代码自动新增了两行.

@property (weak, nonatomic) IBOutlet UITextField *txtName;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *lblMessage;

依旧是拖动,这次选中按钮,将 Touch Down拖到.h 代码里.
最终.h文件代码如下.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *txtName;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *lblMessage;
- (IBAction)btnClick:(id)sender;

@end

.m文件是.h的实现,选择ViewController.m, 添加代码如下.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize txtName;
@synthesize lblMessage;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnClick:(id)sender {
    lblMessage.text = [txtName.text stringByAppendingString:@",Hello"];
}
@end

好,Run!
ok! 跑出来了.

但是感觉不对.虚拟键盘,出来了就不走了,有木有,很脑火.

请教下百度老师.找解决方案.

左侧文件列表选中.xib文件,还是拖控件!(不用吐槽,c#入门的时候也走这条路).

右下选中Tap Gesture Recogize主到 View 里,最终如图.

最终.h 代码如下:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak,nonatomic) IBOutlet UITextField *txtName;
@property (weak,nonatomic) IBOutlet UILabel *lblMessage;
@property (strong,nonatomic) IBOutlet UITapGestureRecognizer *tg;
- (IBAction)btnClick:(id)sender;
@end

并修改.m 代码最终如下:

#import "ViewController.h"
 



@interface ViewController ()
 



@end
 



@implementation ViewController
 
@synthesize txtName;
 
@synthesize lblMessage;
 
@synthesize tg;
 
- (void)viewDidLoad
 
{
 
    [super viewDidLoad];
 
    tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKb:)];
 
    tg.cancelsTouchesInView = NO;
 
    [self.view addGestureRecognizer:tg];
 
    // Do any additional setup after loading the view, typically from a nib.
 
}
 



- (void)didReceiveMemoryWarning
 
{
 
    [super didReceiveMemoryWarning];
 
    // Dispose of any resources that can be recreated.
 
}
 



- (IBAction)btnClick:(id)sender {
 
    lblMessage.text = [txtName.text stringByAppendingString:@",Hello"];
 
}
 



-(void) hideKb:(UITapGestureRecognizer*) t{
 
    [txtName resignFirstResponder];
 
}
 
@end

 

Run! 完成输入后点击空白地方,键盘会消失.

要睡了, 我从1点写到4点,你敢信! 困死.

 

这是我第一次写博文,我希望记录我学习的过程,也希望找到一个伴一起学习,一起研究. 因为总有一些人,比如同学,比如长辈..等等你不愿意去问,不愿意去打捞他们.来找我吧,闭门造车是件很痛苦的事,愿与你一起进步.

 

再吐槽下网络上的一些博文,既然你有心分享,为什么不讲清楚.

  

转载于:https://www.cnblogs.com/igqtdh/archive/2013/04/26/3043919.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值