iOS - ช่วยด้วยครับ แอดไปหน้า Favorite พอปิด iOS Simulator แล้วเปิดใหม อยากไห้มีข้อมูล หน้า Favorite เหมือนตอนแอดครับ!!
ช่วยหน่อยครับ ท่านผู้รู้ พอดีผมกำลังทำ เกี่ยวกับ Favorite แต่ปัญหาคือ เวลาแอดเป็น Favorite แล้ว ปิด iOS Simulator เริ่มใหม่หมด หน้าที่เพิ่งแอดเป็น Favorite ก็ หายไป พอมีวิธีใหมครับ หรือว่าต้องลองกับเครืื่องจริง!!
โค้คทั้งหมดครับ
AppDelegate.h
Code (Objective-C)
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,strong) NSMutableArray *friendArray_Delegate;
@end
AppDelegate.m
Code (Objective-C)
#import "AppDelegate.h"
#import "Friends.h"
@implementation AppDelegate
-(void)setdata
{
self.friendArray_Delegate = [[NSMutableArray alloc]init];
[self.friendArray_Delegate addObject:[[Friends alloc]initWithName:@"Onepen" andPhoneNo:@"081000000" andImageName:[UIImage imageNamed:@"1.jpg"]]];
[self.friendArray_Delegate addObject:[[Friends alloc]initWithName:@"Twopen" andPhoneNo:@"082000000" andImageName:[UIImage imageNamed:@"3.jpg"]]];
[self.friendArray_Delegate addObject:[[Friends alloc]initWithName:@"Threepen" andPhoneNo:@"08300000" andImageName:[UIImage imageNamed:@"4.jpeg"] ]];
[self.friendArray_Delegate addObject:[[Friends alloc]initWithName:@"Fourpen" andPhoneNo:@"084000000" andImageName:[UIImage imageNamed:@"5.jpg" ]]];
[self.friendArray_Delegate addObject:[[Friends alloc]initWithName:@"Fivepen" andPhoneNo:@"0855555555" andImageName:[UIImage imageNamed:@"6.jpg"] ]];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setdata];
// Override point for customization after application launch.
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 throttle down OpenGL ES frame rates. 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 inactive 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
Friends.h
Code (Objective-C)
#import <Foundation/Foundation.h>
@interface Friends : NSObject
@property (nonatomic, strong)NSString *name;
@property (nonatomic, strong)NSString *phone;
@property (nonatomic, strong)UIImage *image;
@property (nonatomic, assign)BOOL favorite;
-(id)initWithName:(NSString *)tempName andPhoneNo:(NSString *)tempPhoneNo andImageName:(UIImage *)tempImage;
@end
Friends.m
Code (Objective-C)
#import "Friends.h"
@implementation Friends
-(id)initWithName:(NSString *)tempName andPhoneNo:(NSString *)tempPhoneNo andImageName:(UIImage *)tempImage
{
self = [super init];
if(self){
self.name = tempName;
self.phone = tempPhoneNo;
self.image = tempImage;
self.favorite = NO;
}
return self;
}
@end
FirstViewController.h
Code (Objective-C)
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *friendArray_First;
@end
FirstViewController.h
Code (Objective-C)
#import "FirstViewController.h"
#import "AppDelegate.h"
#import "Friends.h"
#import "ChannelViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
-(AppDelegate*) appDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.friendArray_First = [[NSMutableArray alloc]init];
self.friendArray_First = [self appDelegate].friendArray_Delegate;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self.tableView reloadData];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.friendArray_First.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//กำหนดว่ามีกี่ section(กลุ่ม)
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//จัดการกับข้อมูลแต่ละ cell
//มันเหมือนวน loop ใส่ข้อความไปแต่ละรอบ
static NSString * CellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Friends *obj = [self.friendArray_First objectAtIndex:indexPath.row];
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton; //เพิ่มปุ่มButton
cell.textLabel.text = [obj name];
cell.detailTextLabel.text = [obj phone];
cell.imageView.image = obj.image;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ChannelViewController *detailv = [[ChannelViewController alloc]initWithfriend:[self.friendArray_First objectAtIndex:indexPath.row]];
[self.navigationController pushViewController:detailv animated:YES];
}
@end
ChannelViewController.h
Code (Objective-C)
#import <UIKit/UIKit.h>
#import "Friends.h"
@interface ChannelViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *pic1;
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UILabel *phone;
@property(nonatomic,strong)Friends *detailf;
-(id)initWithfriend:(Friends *)tempfriends;
@end
ChannelViewController.m
Code (Objective-C)
#import "ChannelViewController.h"
#import "AppDelegate.h"
@interface ChannelViewController ()
@end
@implementation ChannelViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(AppDelegate*) appDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}
-(void)addfaverite
{
if(self.detailf.favorite == NO)
{
self.detailf.favorite = YES;
}
else self.detailf.favorite = NO;
}
-(id)initWithfriend:(Friends *)tempfriends
{
self = [super initWithNibName:@"ChannelViewController" bundle:nil];
if (self) {
// Custom initialization
self.detailf = tempfriends;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.name.text = self.detailf.name;
self.phone.text = self.detailf.phone;
self.pic1.image = self.detailf.image;
UIBarButtonItem *lbarbutton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"28-star.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(addfaverite)];
self.navigationItem.rightBarButtonItem = lbarbutton;
}
FavoriteViewController.h
Code (Objective-C)
#import <UIKit/UIKit.h>
@interface FavoriteViewController : UIViewController<UITabBarControllerDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *Tablef;
@property (strong,nonatomic) NSMutableArray *favArray;
@end
FavoriteViewController.m
Code (Objective-C)
#import "FavoriteViewController.h"
#import "ChannelViewController.h"
#import "AppDelegate.h"
@interface FavoriteViewController ()
@end
@implementation FavoriteViewController
-(AppDelegate*) appDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication]delegate];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.favArray =[[NSMutableArray alloc]init];
for (Friends *i in [self appDelegate].friendArray_Delegate) {
if (i.favorite) {
[self.favArray addObject:i];
}
}
[self.Tablef reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.favArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//กำหนดว่ามีกี่ section(กลุ่ม)
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//จัดการกับข้อมูลแต่ละ cell
//มันเหมือนวน loop ใส่ข้อความไปแต่ละรอบ
static NSString * CellIdentifier = @"Cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
Friends *obj = [self.favArray objectAtIndex:indexPath.row];
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
cell.textLabel.text = [obj name];
cell.detailTextLabel.text = [obj phone];
cell.imageView.image = obj.image;
return cell;
}
@end
ส่วนนี้ตัวโค้ค ทั้งหมดครับ เพื่อลอง Run
http://www.filecondo.com/download_regular.php?file=k143a81uXwPnTag : Mobile, iOS
Date :
2013-07-12 13:34:22
By :
iMonster
View :
1769
Reply :
3
ลอง Capture ภาพประกอบด้วยครับ
Date :
2013-07-12 16:23:42
By :
mr.win
ขอบคุณครับ พอเข้าใจละ อิอิ
Date :
2013-07-16 09:29:31
By :
iMonster
ยังไงหว๋า
Date :
2013-07-16 16:22:50
By :
mr.win
Load balance : Server 05