想要实现输入框中输入一个号码,点击按钮拨号的功能,很简单,让我们以纯代码的形式实现他。
首先,创建一个TextField 和一个UIButton
其次,写一个拨号的方法,与Button相关联
过程如下:
.h文件中
//
// LYXViewController.h
// UITap
//
// Created by liyongxing on 13-7-6.
// Copyright (c) 2013年 liyongxing. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LYXViewController : UIViewController
@property (nonatomic,strong)UITextField * textFiled;
@property (nonatomic,strong)UIButton * button;
@end
//
// LYXViewController.m
// UITap
//
// Created by liyongxing on 13-7-6.
// Copyright (c) 2013年 liyongxing. All rights reserved.
//
#import "LYXViewController.h"
@interface LYXViewController ()
@end
@implementation LYXViewController
-(void)viewDidLoad
{
[super viewDidLoad];
//创建一个输入框
self.textFiled = [[UITextFieldalloc]initWithFrame:CGRectMake(50, 50, 180, 40)];
self.textFiled.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.textFiled];
//创建一个按钮
self.button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(250,50 , 60, 40);
[self.button addTarget:self action:@selector(PhoneButton)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
//拨号的方法,就是定义一个字符串,如果字符串不等于空的情况下,那我们就打开一个拨号的路径,这个路径就是定义的字符串
-(void)PhoneButton
{
NSString * phone = self.textFiled.text;
if (phone != nil)
{
[[UIApplication sharedApplication]openURL:[NSURLURLWithString:[NSString stringWithFormat:@"%@",phone]]];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end