iOS学习-2.据网址显示源码

输入网址,解出源码,显示label

我这里是在第二个界面显示的,用的属性传值。

A界面先从 storyboard 拖个 textfield 和一个 button

.m里面button的方法

//按钮点击方法
- (IBAction)Click:(id)sender {
    //前缀
    NSString *str1 = @"http://";
    //把前缀加到你输入的网址前
    NSString *str2 = [str1 stringByAppendingString:_field.text];
    //解码
    NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:str2] encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",str);
    SecondController *secondView = [[SecondController alloc]init];
    //把解出来的字符串赋给B界面的属性
    secondView.string = str;
    //跳转,我用的navgation跳的
    [self.navigationController pushViewController:secondView animated:YES];
}

B界面定义一个string用于接收

@property(nonatomic,copy)NSString *string;

.m

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *label= [[UILabel alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width ,self.view.frame.size.height)];
    [self.view addSubview:label];
    
    label.numberOfLines = 0 ;
    
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    //接收过来的值赋给label
    label.text = _string;
}

 

你可能感兴趣的:(iOS学习-2.据网址显示源码)