IOS 开发-UI初级 (二 · 后续)View,ViewController实践和应用

上篇博客说到了ViewController和几个控件(UITextView ,UIImageView),接下来就用它们做出个稍微好看一点的界面吧!
最终效果如下图:
这里写图片描述
如图,屏幕上排列了一些《星际争霸2》的人物角色,点击他们进入第二个界面显示介绍。
首先准备9张图片分别命名为img0.png~img8.png,放到工程中。
然后是写代码:

  • MyViewController.h
//
//  MyViewController.h

//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController
@property(nonatomic,strong)NSString * imgName;
@property(nonatomic,strong)NSString * text;
@end
  • ViewController.m
//
//  ViewController.m
//
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "ViewController.h"
#define SCREEN_WIDTH (CGRectGetWidth(self.view.bounds))
#define SCREEN_HEIGHT (CGRectGetHeight(self.view.bounds))
#define BUTTON_WIDETH (SCREEN_WIDTH/3.0-20)
#define BUTTON_HEIGHT (SCREEN_WIDTH/3.0-20)
#import "MyViewController.h"

//将屏幕和按钮的宽度及高度定义为宏,方便使用。

@interface ViewController ()
@property(nonatomic,strong) NSArray * arrayText;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.arrayText=@[@"阿塔尼斯",//这个数组保存角色的介绍信息
                     @"刀锋女王",
                     @"吉姆.雷诺",
                     @"幽灵特工",
                     @"泽拉图",
                     @"阿克图尔斯.蒙斯克",
                     @"圣堂武士",
                     @"泰凯斯.芬利",
                     @"执政官"
                     ];
    UILabel * titLeLatel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 200)];
    titLeLatel.backgroundColor=[UIColor colorWithRed:72/255.0 green:170/255.0 blue:241/255.0 alpha:1.0];
    titLeLatel.font=[UIFont systemFontOfSize:40];
    titLeLatel.textAlignment=NSTextAlignmentCenter;
    //设置文本中心对齐
    [titLeLatel setTextColor:[UIColor whiteColor]];
    titLeLatel.text=@"Star Craft II";
    [self.view addSubview:titLeLatel];
    for (int i=0; i<9; i++) {
        //循环添加9个UIButton控件
        UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
        [button setTag:i+1000];
        //为它们分别设置Tag方便之后配合Tag查找对应的图片。
        [button setFrame:CGRectMake(i%3*(SCREEN_WIDTH/3)+10, i/3*(SCREEN_WIDTH/3.0)+300,BUTTON_WIDETH, BUTTON_HEIGHT)];
        //设置按钮的大小
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"img%d",i]] forState:UIControlStateNormal];
        //设置按钮的背景图片
        [[button layer] setCornerRadius:BUTTON_HEIGHT/2.0];
        //设置背景图的圆角
        [[button layer] setMasksToBounds:YES];
        //应用圆角到视图上
        [button addTarget:self action:@selector(onButtonClick:)  forControlEvents:UIControlEventTouchUpInside];
        [[self view] addSubview:button];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)onButtonClick:(UIButton *)sender{
    MyViewController * controller2 = [[MyViewController alloc]init];
    [controller2 setImgName:[NSString stringWithFormat:@"img%ld",[sender tag]-1000]];//根据Tag值设置即将显示的图片的名字
    [controller2 setText:self.arrayText[sender.tag-1000]];//根据tag值在数组中找到即将显示的介绍信息。
    [self presentViewController:controller2 animated:YES completion:nil];//显示第二个界面
}
@end
  • MyViewController.m
//
//  MyViewController.m

//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "MyViewController.h"
#define SCREEN_WIDTH (CGRectGetWidth(self.view.bounds))
#define SCREEN_HEIGHT (CGRectGetHeight(self.view.bounds))
//同样的宏定义,方便使用
@interface MyViewController ()
@end
@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self view]setBackgroundColor:[UIColor whiteColor]];
    NSLog(@"%@",self.imgName);
    UILabel * titLeLatel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 200)];
    //标题
    titLeLatel.backgroundColor=[UIColor colorWithRed:72/255.0 green:170/255.0 blue:241/255.0 alpha:1.0];
    titLeLatel.font=[UIFont systemFontOfSize:40];
    titLeLatel.textAlignment=NSTextAlignmentCenter;
    [titLeLatel setTextColor:[UIColor whiteColor]];
    [titLeLatel setText:@"角色介绍"];
    [self.view addSubview:titLeLatel];
    UIButton * backButton=[UIButton buttonWithType:UIButtonTypeSystem];
    //返回按钮
    [backButton setFrame:CGRectMake(10, 30, 80, 30)];
    [[backButton titleLabel]setFont:[UIFont systemFontOfSize:25]];
    [backButton setTitle:@"返回" forState:UIControlStateNormal];
    [backButton setTintColor:[UIColor whiteColor]];
    [backButton addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [[self view]addSubview:backButton];
    UIImageView * img=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-SCREEN_WIDTH/6, 150, SCREEN_WIDTH/3, SCREEN_WIDTH/3)];
    [[img layer]setCornerRadius:SCREEN_WIDTH/6];
    [[img layer] setMasksToBounds:YES];
    [img setImage:[UIImage imageNamed:self.imgName]];//取出刚才设置的图片名字,用UIImageView显示出来
    UITextView * textview=[[UITextView alloc]initWithFrame:CGRectMake(0, 300, SCREEN_WIDTH, 150)];
    textview.text=self.text;//取出介绍的内容设置给Label
    textview.textAlignment=NSTextAlignmentCenter;
    textview.editable=NO;
    textview.font=[UIFont systemFontOfSize:30];
    [[self view] addSubview:img];
    [[self view] addSubview:textview];

}

- (void)didReceiveMemoryWarning {

}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
-(void)onButtonClick:(UIButton *)sender{
    [[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];//返回上一个界面
}
@end

上面的例子做到了在两个ViewController之间进行值得传递,这是最简单的一种通过属性来传递数据。今后还会学习其他的传值方式。


The End.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断
iOS开发中上传图片可以采用以下步骤: 1.选择要上传的图片,可以使用系统提供的UIImagePickerController控制器,或者使用第三方库,例如TZImagePickerController。 2.将选中的图片转换为NSData格式。 3.使用NSURLSession或AFNetworking等网络库,将图片数据上传到服务器。 以下是一个简单的上传图片的示例代码: ``` // 选择图片 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.delegate = self; [self presentViewController:imagePicker animated:YES completion:nil]; // 将选中的图片转换为NSData格式 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info { UIImage *selectedImage = info[UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(selectedImage, 0.5); // 上传图片到服务器 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURL *url = [NSURL URLWithString:@"http://example.com/upload.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 处理服务器返回的响应 }]; [uploadTask resume]; [picker dismissViewControllerAnimated:YES completion:nil]; } ``` 其中,upload.php是服务器端接收图片的脚本文件。在服务器端,可以使用PHP等语言来处理上传的图片数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值