ios开发-页面从后往前传值经典例题

本文介绍了一个iOS应用中实现联系人管理的功能,包括添加联系人、显示联系人信息等操作,并展示了如何通过SQLite进行数据持久化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <Foundation/Foundation.h>

@interface ABPerson : NSObject
@property(nonatomic,strong)UIImage *image;
@property(nonatomic,strong)NSString *number;
@end



@implementation ABPerson
-(
void)dealloc{
   
 self.number = nil;
   
 self.image = nil;
}
@end



#import <Foundation/Foundation.h>
#import
"ABPerson.h"
@interface ABPersonInfo : NSObject
+ (
instancetype)defaultManager;

//添加联系人
- (
void)addPersonInfoObject:(ABPerson *)personInfo;
//获取对应下标值的联系人信息
- (
ABPerson *)personInfoObjectAtIndex:(NSUInteger) index;
//获取联系人的个数
- (
NSUInteger)personInfoCount;
@end


#import "ABPersonInfo.h"

@interface ABPersonInfo()
@property (nonatomic ,readonly) NSMutableArray *personInfoList;
@end




@implementation ABPersonInfo

+ (
instancetype)defaultManager
{
   
 static ABPersonInfo *s_ABPersonInfo= nil;
   
 static dispatch_once_t onceToken;
   
 dispatch_once(&onceToken, ^{
       
 if (s_ABPersonInfo == nil) {
            s_ABPersonInfo = [[
ABPersonInfo alloc] init];
        }
    });
   
   
 return s_ABPersonInfo;
}

- (
instancetype)init
{
   
 self = [super init];
   
 if (self) {
       
 //实例化单例对象内的联系人存储机制...
       
 _personInfoList = [[NSMutableArray alloc] init];
    }
   
 return self;
}

//添加联系人
- (
void)addPersonInfoObject:(ABPerson *)personInfo
{
    [
self.personInfoList addObject:personInfo];
      
 NSLog(@"%@",self.personInfoList);
}
//获取对应下标值的联系人信息
- (
ABPerson *)personInfoObjectAtIndex:(NSUInteger) index;
{
   
 if(index >= self.personInfoCount)
       
 return nil;
   
 return self.personInfoList[index];
}
//获取联系人的个数
- (
NSUInteger)personInfoCount
{
   
 return self.personInfoList.count;
}

@end



#import <UIKit/UIKit.h>
#import
 "ABPerson.h"
#import
 "MYYSecondViewController.h"
@interface MYYFirstViewController : UIViewController
@property(nonatomic,strong)UIImageView *headImage;
@property(nonatomic,strong)ABPerson *personInfo;
-(
IBAction)didClickEnterButton:(id)sender;
@end



#import "MYYFirstViewController.h"
#import
 <sqlite3.h>

//创建表格的回调函数
int createCallBack (void *content,int intValue,char **char1,char **char2){
   
 NSLog(@"%s",__func__);
   
 return 1;
}
@interface MYYFirstViewController ()

@end

@implementation MYYFirstViewController{
   
 NSUInteger sum;
   
 NSUInteger sub;
}

- (
id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   
 if (self) {
       
 // Custom initialization
    }
   
 return self;
}

- (
void)viewWillAppear:(BOOL)animated
{
    [
super viewWillAppear:animated];
  
 // NSUInteger count = [[ABPersonInfo defaultManager]personInfoCount];
//    ABPerson *personInfo =  [[ABPersonInfo defaultManager]personInfoObjectAtIndex:0];
//   self.title = personInfo.number;
//    self.headImage.image =personInfo.image;
// 
}

- (
void)viewDidLoad
{
    [
super viewDidLoad];
   
 // Do any additional setup after loading the view from its nib.
   
 UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"3icon"] style:UIBarButtonItemStyleDone target:self action:@selector(_didClickNextPage:)];
   
 self.navigationItem.rightBarButtonItem = rightButton;
   
   
 UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"2icon"] style:UIBarButtonItemStyleDone target:self action:@selector(_didClickNextPage1:)];
   
 self.navigationItem.leftBarButtonItem = rightButton1;
   
     
 UIImage *image = [UIImage imageNamed:@"1"];
    
 self.headImage = [[UIImageView alloc] initWithImage:image];
   
    [
self.view addSubview:self.headImage];
   
   [
 self.headImage setFrame:CGRectMake(120, 120, 80, 80)];
   
   
   
   
 //    //打开文件
  
 NSString *documentPath =NSSearchPathForDirectoriesInDomains(9, 1, 1)[0];

   
 //    //创建文件名

  
 NSString *sqlitePath = [documentPath stringByAppendingString:@"/save.sqlite"];

    
 sqlite3 *db = NULL;
  
   
 //    //打开数据库
       
 int result = sqlite3_open(sqlitePath.UTF8String, &db);
  
   
   
 //创建表个对象
   
 NSString *createSQL = @"create table PersonList(p_number text,p_image text);";
   
 char *error = NULL;
   
    result =
 sqlite3_exec(db, createSQL.UTF8String, createCallBack , @"hehe", &error);
  
}

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


-(
IBAction)didClickEnterButton:(id)sender{
   
   
 MYYSecondViewController *secondVC = [[MYYSecondViewController alloc] initWithNibName:@"MYYSecondViewController" bundle:nil];
        [
self.navigationController pushViewController:secondVC animated:YES];
}


-(
NSUInteger)_didClickNextPage:(NSUInteger)sender{
      
 NSUInteger count = [[ABPersonInfo defaultManager]personInfoCount];
 
   
 if (sum < count) {
          
 ABPerson *personInfo =  [[ABPersonInfo defaultManager]personInfoObjectAtIndex:sum];
         
 self.title = personInfo.number;
         
 self.headImage.image =personInfo.image;
         
 sum++;

    }
   
 return sum;
    }

-(
NSUInteger)_didClickNextPage1:(NSUInteger)sender{
   
 if (sum >0){
   
       
 ABPerson *personInfo =  [[ABPersonInfo defaultManager]personInfoObjectAtIndex:sum-1];
       
 self.title = personInfo.number;
       
 self.headImage.image =personInfo.image;
       
 sum--;
    }
   
 return sum;
}
@end



#import <UIKit/UIKit.h>
#import
 "ABPerson.h"
@interface MYYSecondViewController : UIViewController< UINavigationControllerDelegate, UIImagePickerControllerDelegate,UITextFieldDelegate]] > 

@property(nonatomic,strong)IBOutlet UITextField *numberField;
@property(nonatomic,strong)IBOutlet UIButton *imageButton;
@property(nonatomic,strong)ABPerson *person;
-(
IBAction)didClickButton :(id)sender;


@end


#import "MYYSecondViewController.h"

@interface MYYSecondViewController ()

@property(nonatomic,strong)UIImage *tempImage;
@property(nonatomic,strong)NSString *tempNumber;
@end

@implementation MYYSecondViewController

- (
id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   
 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
   
 if (self) {
       
 // Custom initialization
    }
   
 return self;
}

- (
void)viewDidLoad
{
    [
super viewDidLoad];
   
 // Do any additional setup after loading the view from its nib.
   
 UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(_didClickSave:)];
   
 self.navigationItem.rightBarButtonItem = right;
   
   

   
}


- (
void)viewWillAppear:(BOOL)animated
{
    [
super viewWillAppear:animated];
   
 if (self.person == nil) {
       
 self.person = [[ABPerson alloc] init];
    }
}

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


-(
void)_didClickSave:(UIBarButtonItem *)sender{
   
 ABPerson *person = [[ABPerson alloc]init];
    person.
image = self.tempImage;
    person.
number = self.tempNumber;
    [[
ABPersonInfo defaultManager]addPersonInfoObject:person];
    [
self.navigationController popToRootViewControllerAnimated:YES];
}


-(
IBAction)didClickButton :(id)sender{
   
 UIImagePickerController *pickerVC = [[UIImagePickerController alloc] init];
    [pickerVC
 setAllowsEditing:YES];
    [pickerVC
 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [pickerVC
 setDelegate:self];
    [
self presentViewController:pickerVC animated:YES completion:NULL];
}

//#pragma mark - 图片获取代理对象
- (
void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker
 dismissViewControllerAnimated:YES completion:NULL];
   
 UIImage *image = info[UIImagePickerControllerOriginalImage];
   
 self.tempImage = image;
   
    [
 self.imageButton setBackgroundImage:self.tempImage forState:UIControlStateNormal];
}

- (
BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField
 resignFirstResponder];
return YES;
}

-(
BOOL)textFieldShouldEndEditing:(UITextField *)textField{
 
       
 self.tempNumber =self.numberField.text;
   
        
 return YES;
}
@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值