iOS中微信界面的设计 跳转 与 传值

1.创建一个Single View App
2.添加一个Cocoa class ,命名为WXTableViewController

     WXTableViewController.h 文件 
    #import <UIKit/UIKit.h>

@interface WXTableViewController : UITableViewController

@end

WXTableViewController.m文件


#import "WXTableViewController.h"
#import "weixinMondel.h"
#import "weixinCell.h"
#import "DefaultInstance.h"
#import "nextViewController.h"
@interface WXTableViewController ()
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,strong)UILabel *lable;
@end

@implementation WXTableViewController
{
    nextViewController *_nextViewController;
}
-(UILabel *)lable{
    if(!_lable)
    {
        _lable=[[UILabel alloc]initWithFrame:CGRectMake(70, 80, 300, 20)];
        _lable.textColor=[UIColor blackColor];
        [DefaultInstance sharedInstance].str=@"微信号:fjdgjfdl;gjf;gjf;gj;fhj';h;";
        _lable.text=[DefaultInstance sharedInstance].str;
        _lable.font=[UIFont systemFontOfSize:20];
    }
    return _lable;
}
#pragma mark-
#pragma mark-懒加载数据
-(NSArray *)dataArray{
    if(nil==_dataArray)
    {
        //路径
        NSString *path=[[NSBundle mainBundle]pathForResource:@"wxinfo.plist" ofType:nil];
        //读取
        NSArray *tempArray=[NSArray arrayWithContentsOfFile:path];
        //创建可变数组
        NSMutableArray *mutable=[NSMutableArray array];
        //转为字典存入可变数组
        for(NSDictionary *dict in tempArray)
        {
            weixinMondel *model=[weixinMondel weixinMondelWithDict:dict];
            [mutable addObject:model];
        }
        //把可变数组赋值给_dataArray
        _dataArray=mutable;
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置tableView cell 的行高
    self.tableView.rowHeight=65;
    //设置当前视图关联的导航项的标题
    self.navigationItem.title=@"微信";
    [self.navigationController   setToolbarHidden:NO];
    //toolbar
    UIBarButtonItem *spaceItem=[[UIBarButtonItem alloc]initWithTitle:@"  " style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    spaceItem.tag=4;
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"right10.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    item1.tag=0;
    UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"right11.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    item2.tag=1;
    UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"right12.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    item3.tag=2;
    UIBarButtonItem *item4=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"right13.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    item4.tag=3;
    NSArray *itemArray=[NSArray arrayWithObjects:spaceItem,item1,spaceItem,item2,spaceItem,item3,spaceItem,item4,nil];
    self.toolbarItems=itemArray;
    
    [self.view addSubview:self.lable];
}
- (void)tapped:(UIBarButtonItem *)sender{
    switch (sender.tag) {
        case 0:{} break;
        case 1:{} break;
        case 2:{} break;
        case 3:{} break;
        case 4:{} break;
        default:
            break;
    }
    
}

   

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSInteger rows=0;
    if(section==0)
    {
        rows=1;
    }
    else if(section==1)
    {
        rows=1;
    }
    else if(section==2)
    {
        rows=4;
    }
    else
    {
        rows=1;
    }
    return rows;
}

//每一行的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //定义重用标识符
    static NSString *identifier=@"weixinCell";
    //到缓存池中去取
    weixinCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    //进行判断
    if(nil==cell)
    {
        cell = [[weixinCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //cell.accessoryType=UITableViewCellAccessoryCheckmark;//添加附件
    /*indexPath.row  行
      indexPath.section 组
      通过组和行可以唯一定位*/
    //把组取出来
    NSInteger sectionIndex=indexPath.section;
    if(sectionIndex==0)//第0组
    {
        //判断第0组的第几行
        NSInteger rowIndex=indexPath.row;
        if(rowIndex==0)
        {
            //给cell 传递数据
            weixinMondel *weixinModel=self.dataArray[indexPath.row];
            cell.weixinModel=weixinModel;
        }
    }
    else if(indexPath.section==1)//两行
    {
        //判断第0组的第几行
        NSInteger rowIndex=indexPath.row;
        if(rowIndex==0)
        {
            //给cell 传递数据
            weixinMondel *weixinModel=self.dataArray[indexPath.row+1];
            cell.weixinModel=weixinModel;
        }
    }
    else if(indexPath.section==2)//三行
    {
        NSInteger rowIndex=indexPath.row;
        if(rowIndex==0)
        {
            //给cell 传递数据
            weixinMondel *weixinModel=self.dataArray[indexPath.row+2];
            cell.weixinModel=weixinModel;
        }
        if(rowIndex==1)
        {
            //给cell 传递数据
            weixinMondel *weixinModel=self.dataArray[indexPath.row+2];
            cell.weixinModel=weixinModel;
        }
        if(rowIndex==2)
        {
            //给cell 传递数据
            weixinMondel *weixinModel=self.dataArray[indexPath.row+2];
            cell.weixinModel=weixinModel;
        }
        if(rowIndex==3)
        {
            //给cell 传递数据
            weixinMondel *weixinModel=self.dataArray[indexPath.row+2];
            cell.weixinModel=weixinModel;
        }
    }
    
    else if(indexPath.section==3)
    {
        NSInteger rowIndex=indexPath.row;
        if(rowIndex==0)
        {
            //给cell 传递数据
            weixinMondel *weixinModel=self.dataArray[indexPath.row+6];
            cell.weixinModel=weixinModel;
        }
    }
    return cell;
}
//当用户完成选中表格中的某行时激发该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath
{
   
    nextViewController *_nextViewController=[[nextViewController alloc]init];
    [DefaultInstance sharedInstance].str=_lable.text;
    [self.navigationController pushViewController:_nextViewController animated:YES];
    
}



-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //接受反向传值
   
    _lable.text=[DefaultInstance sharedInstance].str;
}

@end

3.创建wxinfo.plist 文件,并且设计如图所示
这里写图片描述
4.创建Cocoa Touch Class,命名为 weixinMondel

                weixinMonde.h文件

#import <Foundation/Foundation.h>

@interface weixinMondel : NSObject<NSCoding>
//表示文本内容
@property(nonatomic,copy) NSString *text;
//表示头像
@property(nonatomic,copy) NSString *icon;
//表示名称
@property(nonatomic,copy) NSString *name;
//表示配图
@property(nonatomic,copy) NSString *picture;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)weixinMondelWithDict:(NSDictionary *)dict;
@end

                 weixinMonde.m

#import "weixinMondel.h"

@implementation weixinMondel
-(instancetype)initWithDict:(NSDictionary *)dict
{
    if(self=[super init])
    {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
+(instancetype)weixinMondelWithDict:(NSDictionary *)dict
{
    return [[self alloc]initWithDict:dict];
}
// 当将一个自定义对象保存到文件的时候就会调用该方法
// 在该方法中说明如何存储自定义对象的属性
// 也就说在该方法中说清楚存储自定义对象的哪些属性
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.icon forKey:@"icon"];
    [aCoder encodeObject:self.picture forKey:@"picture"];
    [aCoder encodeObject:self.text forKey:@"text"];
}

// 当从文件中读取一个对象的时候就会调用该方法
// 在该方法中说明如何读取保存在文件中的对象
// 也就是说在该方法中说清楚怎么读取文件中的对象
- (id)initWithCoder:(NSCoder *)aDecoder
{
    //注意:在构造方法中需要先初始化父类的方法
    if (self=[super init]) {
        self.name=[aDecoder decodeObjectForKey:@"name"];
        self.icon=[aDecoder decodeObjectForKey:@"icon"];
        self.picture=[aDecoder decodeObjectForKey:@"picture"];
        self.text=[aDecoder decodeObjectForKey:@"text"];
    }
    return self;
}


@end

5.创建Cocoa Touch Class,命名为weixinCell

                      weixinCell.h

#import <UIKit/UIKit.h>
@class weixinMondel;
@interface weixinCell : UITableViewCell
@property(nonatomic,strong)weixinMondel *weixinModel;
@end

                       weixinCell.m 

#import "weixinCell.h"
#import "weixinMondel.h"
@interface weixinCell()

@property (nonatomic,weak)UIImageView *userimageView;
@property (nonatomic,weak)UIImageView *pictureImageView;
@property (nonatomic,weak)UILabel *nameLabel;
@property (nonatomic,weak)UILabel *contentLabel;






@end

@implementation weixinCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self=[super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //初始化cell界面
        [self setupUI];
    }
    return self;
}
#pragma mark--
#pragma mark--
-(void) setupUI
{
    //1.用户头像的userimageView
    UIImageView *userimageView=[[UIImageView alloc]init];
    self.userimageView=userimageView;
    //添加到cell的contentView
    [self.contentView addSubview:userimageView];
    
    //2.用户名称的UILable
    UILabel *nameLabel=[[UILabel alloc]init];
    //self.nameLabel=nameLabel;
    [self.contentView addSubview:nameLabel];
    
    //3.文本内容的UILable
    UILabel *contentLabel=[[UILabel alloc]init];
    self.contentLabel=contentLabel;
    [self.contentView addSubview:contentLabel];
    //4.配图的UIImageView
    UIImageView *pictureImageView=[[UIImageView alloc]init];
    self.pictureImageView=pictureImageView;
    [self.contentView addSubview:pictureImageView];
}

#pragma mark--
#pragma mark--设置数据
-(void)setWeixinModel:(weixinMondel *)weixinModel
{
    _weixinModel=weixinModel;
    //为cell中的控件进行赋值
    self.userimageView.image=[UIImage imageNamed:weixinModel.icon];
    self.nameLabel.text=weixinModel.name;
    self.contentLabel.text=weixinModel.text;
    self.pictureImageView.image=[UIImage imageNamed:weixinModel.picture];
    
    
    
    
    //计算每个控件的frame
    //计算头像的frame
    CGFloat margin =10;
    CGFloat userImageWidth =50;
    _userimageView.frame=CGRectMake(margin, margin, userImageWidth, userImageWidth);
    
    //计算每个控件的text
    CGFloat contentLabelx=CGRectGetMaxX(_userimageView.frame)+margin;
    CGFloat contentLabelHeight=20;
    CGFloat contentLabelWidth =150;
    CGFloat contentLabely=(userImageWidth-contentLabelHeight)/2+margin;
    _contentLabel.frame=CGRectMake(contentLabelx, contentLabely, contentLabelWidth, contentLabelHeight);
    
    
    //计算每个控件的name
    CGFloat nameLabelx=CGRectGetMaxX(_userimageView.frame)+margin;
    CGFloat nameLabelHeight=20;
    CGFloat nameLabelWidth =150;
    CGFloat nameLabely=(userImageWidth-nameLabelHeight)/2+margin+contentLabelHeight;
    _nameLabel.frame=CGRectMake(nameLabelx, nameLabely, nameLabelWidth, nameLabelHeight);
    
    //计算每个控件的配图
    CGFloat pictureImageViewx=nameLabelx+150+150;
    CGFloat pictureImageViewHeight=20;
    CGFloat pictureImageViewWidth=20;
    CGFloat pictureImageViewy=contentLabely+margin;
    _pictureImageView.frame=CGRectMake(pictureImageViewx,pictureImageViewy, pictureImageViewWidth, pictureImageViewHeight);
    
}

@end

6.为了实现界面传值

                       AppDelegate.h文件

#import <UIKit/UIKit.h>
#import "WXTableViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic)WXTableViewController *viewController;
@property (strong,nonatomic)UINavigationController *naviController;

@end

                   AppDelegate.m文件
#import "AppDelegate.h"
#import "WXTableViewController.h"
@interface AppDelegate ()
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.viewController=[[WXTableViewController alloc]initWithStyle:UITableViewStyleGrouped];
    //创建uinavigationcontroller对象
    //该uinavigationcontroller以self.viewcontroller为试图赞最底层控件
    self.naviController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
    //设置窗口以self.navigationcontroller为跟试图控制器
    self.window.rootViewController=self.naviController;
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end

7.当点击tableview中的某一行时,跳转的界面

        nextViewController.h
#import <UIKit/UIKit.h>

@interface nextViewController : UIViewController
{
    UIToolbar *toolBar;
}

@end

        nextViewController.m
#import "nextViewController.h"
#import "DefaultInstance.h"
#import "WXTableViewController.h"
@interface nextViewController ()<UITableViewDelegate>
@property (strong,nonatomic) UITextField *textField;
@property (strong,nonatomic) UIButton *btn;
@end

@implementation nextViewController
-(UITextField *)textField{
    if(!_textField)
    {
        _textField=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
        _textField.textColor=[UIColor redColor];
        _textField.font=[UIFont systemFontOfSize:20];
        _textField.borderStyle=UITextBorderStyleLine;
        //单例传值--接收并显示
        _textField.text=[DefaultInstance sharedInstance].str;
    }
    return _textField;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.textField];
    //关于toolBar
    [self.navigationController   setToolbarHidden:NO];
    //toolbar
    UIBarButtonItem *spaceItem=[[UIBarButtonItem alloc]initWithTitle:@"  " style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    spaceItem.tag=4;
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"right10.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    item1.tag=0;
    UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"right11.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    item2.tag=1;
    UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"right12.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    item3.tag=2;
    UIBarButtonItem *item4=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"right13.png"] style:UIBarButtonItemStylePlain target:self action:@selector(tapped:)];
    item4.tag=3;
    NSArray *itemArray=[NSArray arrayWithObjects:spaceItem,item1,spaceItem,item2,spaceItem,item3,spaceItem,item4,nil];
    self.toolbarItems=itemArray;
   

}
- (void)tapped:(UIBarButtonItem *)sender{
    switch (sender.tag) {
        case 0:{} break;
        case 1:{} break;
        case 2:{} break;
        case 3:{} break;
        case 4:{} break;
        default:
            break;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillDisappear:(BOOL)animated
{
    [DefaultInstance sharedInstance].str=self.textField.text;
    NSLog(@"self.textField.text");
}

@end

8.界面传值

         DefaultInstance.h
#import <Foundation/Foundation.h>

@interface DefaultInstance : NSObject
@property(nonatomic,weak)NSString *str;
+(instancetype)sharedInstance;
@end

        DefaultInstance.m
#import "DefaultInstance.h"

@implementation DefaultInstance
//通过类方法创建单例对象
+(instancetype)sharedInstance
{
    static DefaultInstance   *sharedVC=nil;
    if(sharedVC==nil)
    {
        sharedVC=[[DefaultInstance alloc]init];
    }
    return sharedVC;
}
@end

9.注意添加的图片的分类 Assets.xcassets, 如图所示
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值