接前一篇,因為有連續的UIViewController動態出現,但希望可一次回到最原始的Root UIViewController,在此需要借助NavigationBar的幫助,但是在使用時卻將navigationBar給隱藏起來。
以下是改良的實作
1. 在Storyboard上加入一個Navigation Bar
2. 修改mainViewController.m中動態UIViewController聯結的部分
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setGoFirstViewButton];
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void) activeView
{
firstView = [[FirstViewController alloc] init];
firstView.view.backgroundColor = [UIColor blueColor];
[firstView setValue:@"FirstView" forKey:@"transferString"];
NSNumber *number = [[NSNumber alloc] initWithInt:1];
[firstView setValue:number forKey:@"UIViewIndex"];
//[self presentViewController:firstView animated:YES completion:^{}];
[self.navigationController pushViewController:firstView animated:YES];
}
3. 將FirstViewController.m的動態UIViewController聯結的部分也需修改
- (void) goBackMainViewController
{
//int number = [UIViewIndex integerValue];
NSArray *viewControllers = self.navigationController.viewControllers;
[self.navigationController popToViewController: [viewControllers objectAtIndex:0] animated: YES];
}
- (void) activeF2View
{
f2View = [[FirstViewController alloc] init];
f2View.view.backgroundColor = [UIColor orangeColor];
[f2View setValue:@"F2View" forKey:@"transferString"];
//NSNumber *number = [[NSNumber alloc] initWithInt:([UIViewIndex integerValue]+1)];
//[f2View setValue:number forKey:@"UIViewIndex"];
//[self presentViewController:f2View animated:NO completion:^{}];
[self.navigationController pushViewController:f2View animated:YES];
}
4. 結果就在於連續使用動態UIViewController後,可直接回到Root UIViewController
2013年1月26日 星期六
2013年1月25日 星期五
動態產生 UIViewController (五)
根據前一篇稍加修改,增加一個對FirstViewController自己的動態聯結,因此可以無限的動態UIViewController,而回到最頂層也是要一級一級的回去。
1. 只在FirstViewController.h加入一個FirstViewController的變數
@property (nonatomic) FirstViewController *f2View;
2. 在FirstViewController.m加入聯結自己的Button
@synthesize f2View;
- (void)setGoF2ViewButton // 動態產生一個的Button
{
UIButton *startSayStoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//動態產生一個RoundedRect 形式的 Button
startSayStoryButton.frame = CGRectMake(0,0, 200, 100); // 大小
//_helloActionButton.center = self.view.center;
[startSayStoryButton setCenter:CGPointMake(500, 300)];//位置放在x=500, y=300的位置
[startSayStoryButton addTarget:self action:@selector(activeF2View) forControlEvents:UIControlEventTouchUpInside];
//設定Button動作呼叫的function在 onHelloActionButton,方式為按下
//_helloActionButton.= @"Action Button";
[startSayStoryButton setTitle:@"To F2View" forState:UIControlStateNormal];
//將動態Button上放置Action Button這兩個字
[self.view addSubview:startSayStoryButton];
//將動態Button放到View上展出
}
- (void) activeF2View
{
f2View = [[FirstViewController alloc] init];
f2View.view.backgroundColor = [UIColor orangeColor];
[f2View setValue:@"F2View" forKey:@"transferString"];
[self presentViewController:f2View animated:YES completion:^{}];
}
3. 結果圖
1. 只在FirstViewController.h加入一個FirstViewController的變數
@property (nonatomic) FirstViewController *f2View;
2. 在FirstViewController.m加入聯結自己的Button
@synthesize f2View;
- (void)setGoF2ViewButton // 動態產生一個的Button
{
UIButton *startSayStoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//動態產生一個RoundedRect 形式的 Button
startSayStoryButton.frame = CGRectMake(0,0, 200, 100); // 大小
//_helloActionButton.center = self.view.center;
[startSayStoryButton setCenter:CGPointMake(500, 300)];//位置放在x=500, y=300的位置
[startSayStoryButton addTarget:self action:@selector(activeF2View) forControlEvents:UIControlEventTouchUpInside];
//設定Button動作呼叫的function在 onHelloActionButton,方式為按下
//_helloActionButton.= @"Action Button";
[startSayStoryButton setTitle:@"To F2View" forState:UIControlStateNormal];
//將動態Button上放置Action Button這兩個字
[self.view addSubview:startSayStoryButton];
//將動態Button放到View上展出
}
- (void) activeF2View
{
f2View = [[FirstViewController alloc] init];
f2View.view.backgroundColor = [UIColor orangeColor];
[f2View setValue:@"F2View" forKey:@"transferString"];
[self presentViewController:f2View animated:YES completion:^{}];
}
3. 結果圖
2013年1月24日 星期四
動態產生 UIViewController (四)
連續使用兩層的動態UIViewController
1. 開啓新的專案
2.新增兩個UIViewController的Class檔
3. 首先在mainViewController.h加入變數
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface mainViewController : UIViewController
@property (nonatomic) FirstViewController *firstView;
@end
4. 在mainViewController.m加入到下一級的程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize firstView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setGoFirstViewButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setGoFirstViewButton // 動態產生一個的Button
{
UIButton *startSayStoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//動態產生一個RoundedRect 形式的 Button
startSayStoryButton.frame = CGRectMake(0,0, 100, 100); // 大小
//_helloActionButton.center = self.view.center;
[startSayStoryButton setCenter:CGPointMake(300, 300)];//位置放在x=150, y=50的位置
[startSayStoryButton addTarget:self action:@selector(activeView) forControlEvents:UIControlEventTouchUpInside];
//設定Button動作呼叫的function在 onHelloActionButton,方式為按下
//_helloActionButton.= @"Action Button";
[startSayStoryButton setTitle:@"To FirstView" forState:UIControlStateNormal];
//將動態Button上放置Action Button這兩個字
[self.view addSubview:startSayStoryButton];
//將動態Button放到View上展出
}
- (void) activeView
{
firstView = [[FirstViewController alloc] init];
firstView.view.backgroundColor = [UIColor blueColor];
[firstView setValue:@"FirstView" forKey:@"transferString"];
[self presentViewController:firstView animated:YES completion:^{}];
}
@end
5. 在FirstViewController.h加入變數
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController
{
NSString *transferString;
}
@property (nonatomic) SecondViewController *secondView;
@end
6. 在FirstViewController.m加入程式碼
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize secondView;
- (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.
[self setGoBackButton];
[self setGoSecondViewButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) setGoBackButton
{
//NSLog(@"%@", transferString);
//self.view.backgroundColor = [UIColor yellowColor];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setFrame:CGRectMake(10, 10, 200, 50)];
aButton.backgroundColor = [UIColor redColor];
//[aButton setTitle:@"Go Back" forState:UIControlStateNormal];
[aButton setTitle:@"回去前一級" forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(goBackMainViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
}
- (void) goBackMainViewController
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)setGoSecondViewButton // 動態產生一個的Button
{
UIButton *startSayStoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//動態產生一個RoundedRect 形式的 Button
startSayStoryButton.frame = CGRectMake(0,0, 200, 100); // 大小
//_helloActionButton.center = self.view.center;
[startSayStoryButton setCenter:CGPointMake(300, 300)];//位置放在x=150, y=50的位置
[startSayStoryButton addTarget:self action:@selector(activeView) forControlEvents:UIControlEventTouchUpInside];
//設定Button動作呼叫的function在 onHelloActionButton,方式為按下
//_helloActionButton.= @"Action Button";
[startSayStoryButton setTitle:@"To SecondView" forState:UIControlStateNormal];
//將動態Button上放置Action Button這兩個字
[self.view addSubview:startSayStoryButton];
//將動態Button放到View上展出
}
- (void) activeView
{
secondView = [[SecondViewController alloc] init];
secondView.view.backgroundColor = [UIColor greenColor];
[secondView setValue:@"SecondView" forKey:@"transferString"];
[self presentViewController:secondView animated:YES completion:^{}];
}
@end
7. 在SecondViewController.h加入變數
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
{
NSString *transferString;
}
@end
8.在SecondViewController.m加入程式碼
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (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.
[self setGoBackButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) setGoBackButton
{
//NSLog(@"%@", transferString);
//self.view.backgroundColor = [UIColor yellowColor];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setFrame:CGRectMake(10, 10, 200, 50)];
aButton.backgroundColor = [UIColor redColor];
//[aButton setTitle:@"Go Back" forState:UIControlStateNormal];
[aButton setTitle:@"回去前一級" forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(goBackMainViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
}
- (void) goBackMainViewController
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
@end
9. 結果圖
1. 開啓新的專案
2.新增兩個UIViewController的Class檔
3. 首先在mainViewController.h加入變數
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface mainViewController : UIViewController
@property (nonatomic) FirstViewController *firstView;
@end
4. 在mainViewController.m加入到下一級的程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize firstView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setGoFirstViewButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setGoFirstViewButton // 動態產生一個的Button
{
UIButton *startSayStoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//動態產生一個RoundedRect 形式的 Button
startSayStoryButton.frame = CGRectMake(0,0, 100, 100); // 大小
//_helloActionButton.center = self.view.center;
[startSayStoryButton setCenter:CGPointMake(300, 300)];//位置放在x=150, y=50的位置
[startSayStoryButton addTarget:self action:@selector(activeView) forControlEvents:UIControlEventTouchUpInside];
//設定Button動作呼叫的function在 onHelloActionButton,方式為按下
//_helloActionButton.= @"Action Button";
[startSayStoryButton setTitle:@"To FirstView" forState:UIControlStateNormal];
//將動態Button上放置Action Button這兩個字
[self.view addSubview:startSayStoryButton];
//將動態Button放到View上展出
}
- (void) activeView
{
firstView = [[FirstViewController alloc] init];
firstView.view.backgroundColor = [UIColor blueColor];
[firstView setValue:@"FirstView" forKey:@"transferString"];
[self presentViewController:firstView animated:YES completion:^{}];
}
@end
5. 在FirstViewController.h加入變數
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface FirstViewController : UIViewController
{
NSString *transferString;
}
@property (nonatomic) SecondViewController *secondView;
@end
6. 在FirstViewController.m加入程式碼
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize secondView;
- (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.
[self setGoBackButton];
[self setGoSecondViewButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) setGoBackButton
{
//NSLog(@"%@", transferString);
//self.view.backgroundColor = [UIColor yellowColor];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setFrame:CGRectMake(10, 10, 200, 50)];
aButton.backgroundColor = [UIColor redColor];
//[aButton setTitle:@"Go Back" forState:UIControlStateNormal];
[aButton setTitle:@"回去前一級" forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(goBackMainViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
}
- (void) goBackMainViewController
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
- (void)setGoSecondViewButton // 動態產生一個的Button
{
UIButton *startSayStoryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//動態產生一個RoundedRect 形式的 Button
startSayStoryButton.frame = CGRectMake(0,0, 200, 100); // 大小
//_helloActionButton.center = self.view.center;
[startSayStoryButton setCenter:CGPointMake(300, 300)];//位置放在x=150, y=50的位置
[startSayStoryButton addTarget:self action:@selector(activeView) forControlEvents:UIControlEventTouchUpInside];
//設定Button動作呼叫的function在 onHelloActionButton,方式為按下
//_helloActionButton.= @"Action Button";
[startSayStoryButton setTitle:@"To SecondView" forState:UIControlStateNormal];
//將動態Button上放置Action Button這兩個字
[self.view addSubview:startSayStoryButton];
//將動態Button放到View上展出
}
- (void) activeView
{
secondView = [[SecondViewController alloc] init];
secondView.view.backgroundColor = [UIColor greenColor];
[secondView setValue:@"SecondView" forKey:@"transferString"];
[self presentViewController:secondView animated:YES completion:^{}];
}
@end
7. 在SecondViewController.h加入變數
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
{
NSString *transferString;
}
@end
8.在SecondViewController.m加入程式碼
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (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.
[self setGoBackButton];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) setGoBackButton
{
//NSLog(@"%@", transferString);
//self.view.backgroundColor = [UIColor yellowColor];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setFrame:CGRectMake(10, 10, 200, 50)];
aButton.backgroundColor = [UIColor redColor];
//[aButton setTitle:@"Go Back" forState:UIControlStateNormal];
[aButton setTitle:@"回去前一級" forState:UIControlStateNormal];
[aButton addTarget:self action:@selector(goBackMainViewController) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
}
- (void) goBackMainViewController
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
@end
9. 結果圖
2013年1月23日 星期三
IOS遊戲資料儲存的方法(一)
此處參考了【黑米GameDev街区】
原文链接: http://www.himigame.com/iphone-cocos2d/513.html
的程式碼。總共有四種方法
1. NSKeyedArchiver(加密形式)
2. NSUserDefaults
3. Write写入方式
4. SQLite3
但是iOS程序设计中常用到的一些数据持久化方法還有:Binary files, Property List, NSCoder, Core Data, etc.
第一種方法的相關資料
本來 NSMutableArray 及 NSMutableDictionary 都有 writeToFile: 功能:
NSKeyedArchiver类中的archiveRootObject:toFile:方法将数据对象存储到磁盘上,如:
通过NSKeyedUnarchiver类中的unArchiveObjectWithFile:方法将创建的归档文件读入执行的程序中,如:
实现NSKeyedArchiver类和NSKeyedUnarchiver类操作的类必须遵守<NSCoding>协议,实现
encodeWithCoder: // 编码方法
initWithCoder: // 解码方法
PS : 对于基本的Objective-C类(NSString, NSArray, NSDictionary, NSSet, NSDate, NSNumber, NSData)则使用
encodeObject:forKey:编码方法和decodeObject:forKey:解码方法:

基本參考程式碼
方法是為自定義的物件建立:
本次實作,將只用基本方法來做,先不使用太複雜的方法。
以下是我再將第一種的程式碼重新改寫的實作,
1. 開啓一個新的專案
2. 在Storyboard加上兩個button
3. 加入兩個圖檔來做資料顯示
4. 在mainViewController.h加入Array變數
#import <UIKit/UIKit.h>
@interface mainViewController : UIViewController
@property (nonatomic) NSMutableArray *saveArray;
@end
5. 在mainViewController.m加入程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize saveArray;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
saveArray = [[NSMutableArray alloc] init];
[self builtImageView:1001];
[self builtImageView:1002];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveAndClean:(id)sender {
[self removeSubView];
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [Path stringByAppendingPathComponent:@"saveDatatest.test"];
[NSKeyedArchiver archiveRootObject:saveArray toFile:filename];
[saveArray removeAllObjects];
}
- (IBAction)readAndRestore:(id)sender {
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [Path stringByAppendingPathComponent:@"saveDatatest.test"];
saveArray = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
int count = [saveArray count];
for (int i=0;i<count ;i++)
{
UIImageView *temp = [saveArray objectAtIndex:i];
[self.view addSubview:temp];
}
[saveArray removeAllObjects];
}
- (void) builtImageView:(int) picNumber
{
UIImageView *tt = [[UIImageView alloc] initWithFrame:CGRectMake(120 + (picNumber%100)*100, 320 , 100, 100)];
[tt setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",picNumber]]];
tt.tag = picNumber;
[self.view addSubview:tt];
}
- (void) removeSubView //:(NSInteger *)tagNum
{
for(UIView *subview in [self.view subviews]) {
if([subview isKindOfClass:[UIImageView class]]) {
//if (tagNum == subview.tag) {
[saveArray addObject:subview];
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
}
@end
5. 結果顯示,先執行左邊,就會存到App的Document目錄下,然後再用右邊讀取並回復原狀。
6.暫存目錄位置,因為App的暫存區為代碼,其下的Documents有儲存的檔案。
的程式碼。總共有四種方法
1. NSKeyedArchiver(加密形式)
2. NSUserDefaults
3. Write写入方式
4. SQLite3
但是iOS程序设计中常用到的一些数据持久化方法還有:Binary files, Property List, NSCoder, Core Data, etc.
第一種方法的相關資料
本來 NSMutableArray 及 NSMutableDictionary 都有 writeToFile: 功能:
NSString *fullPath = [GameViewController getFileWithPath:PLAYER_DATA_FILE]; [playerArray writeToFile:fullPath atomically:NO];但可惜不支援自定義的物件。因此使用NSKeyedArchiver的方法。
NSKeyedArchiver类中的archiveRootObject:toFile:方法将数据对象存储到磁盘上,如:
NSDictionary *glossary = [...]; [NSKeyedArchiver archiveRootObject:glossary toFile:@"file"];
通过NSKeyedUnarchiver类中的unArchiveObjectWithFile:方法将创建的归档文件读入执行的程序中,如:
NSDictionary *glossary = [NSKeyedUnarchiver unArchiveObjectWithFile:@"file"];
实现NSKeyedArchiver类和NSKeyedUnarchiver类操作的类必须遵守<NSCoding>协议,实现
encodeWithCoder: // 编码方法
initWithCoder: // 解码方法
PS : 对于基本的Objective-C类(NSString, NSArray, NSDictionary, NSSet, NSDate, NSNumber, NSData)则使用
encodeObject:forKey:编码方法和decodeObject:forKey:解码方法:
基本參考程式碼
方法是為自定義的物件建立:
- (id)initWithCoder:(NSCoder *)decoder; - (void)encodeWithCoder:(NSCoder *)encoder;這兩個分別是 initWithCoder: 還原數據及 encodeWithCoder: 儲存數據。示範如下:
- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
level = [decoder decodeIntForKey:PLAYER_LEVEL];
hp = [decoder decodeIntForKey:PLAYER_HP];
mp = [decoder decodeIntForKey:PLAYER_MP];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeInt:level forKey:PLAYER_LEVEL];
[encoder encodeInt:hp forKey:PLAYER_HP];
[encoder encodeInt:mp forKey:PLAYER_MP];
}
本次實作,將只用基本方法來做,先不使用太複雜的方法。
以下是我再將第一種的程式碼重新改寫的實作,
1. 開啓一個新的專案
2. 在Storyboard加上兩個button
3. 加入兩個圖檔來做資料顯示
4. 在mainViewController.h加入Array變數
#import <UIKit/UIKit.h>
@interface mainViewController : UIViewController
@property (nonatomic) NSMutableArray *saveArray;
@end
5. 在mainViewController.m加入程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize saveArray;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
saveArray = [[NSMutableArray alloc] init];
[self builtImageView:1001];
[self builtImageView:1002];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveAndClean:(id)sender {
[self removeSubView];
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [Path stringByAppendingPathComponent:@"saveDatatest.test"];
[NSKeyedArchiver archiveRootObject:saveArray toFile:filename];
[saveArray removeAllObjects];
}
- (IBAction)readAndRestore:(id)sender {
NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filename = [Path stringByAppendingPathComponent:@"saveDatatest.test"];
saveArray = [NSKeyedUnarchiver unarchiveObjectWithFile: filename];
int count = [saveArray count];
for (int i=0;i<count ;i++)
{
UIImageView *temp = [saveArray objectAtIndex:i];
[self.view addSubview:temp];
}
[saveArray removeAllObjects];
}
- (void) builtImageView:(int) picNumber
{
UIImageView *tt = [[UIImageView alloc] initWithFrame:CGRectMake(120 + (picNumber%100)*100, 320 , 100, 100)];
[tt setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",picNumber]]];
tt.tag = picNumber;
[self.view addSubview:tt];
}
- (void) removeSubView //:(NSInteger *)tagNum
{
for(UIView *subview in [self.view subviews]) {
if([subview isKindOfClass:[UIImageView class]]) {
//if (tagNum == subview.tag) {
[saveArray addObject:subview];
[subview removeFromSuperview];
} else {
// Do nothing - not a UIButton or subclass instance
}
}
}
@end
5. 結果顯示,先執行左邊,就會存到App的Document目錄下,然後再用右邊讀取並回復原狀。
6.暫存目錄位置,因為App的暫存區為代碼,其下的Documents有儲存的檔案。
2013年1月22日 星期二
iOS App第二次被Apple退件
在前一篇所說被退件後,順便大幅增修內容,因此原本的截圖與App的內容稍稍不同,這樣也被Apple找出來。看來Apple的審查機制還是蠻嚴格的,因此不可存有僥倖的心理,所以寫下這篇來警惕自己。
現在只好也將截圖重新做一遍,再一次上傳了。只不過,這次不用再上傳App了,看來只有截圖有問題而已。
現在只好也將截圖重新做一遍,再一次上傳了。只不過,這次不用再上傳App了,看來只有截圖有問題而已。
2013年1月21日 星期一
使用TextField實作文字輸入(三)
沿用前一篇的專案,加入一個動態設定的TextField,實作中會用到UITextFieldDelegate,因此也必須瞭解UITextFieldDelegate的使用。此處可參考UITextFieldDelegate Protocol Reference。
一般我們通常會用到三個method
1. -(void)textFieldDidBeginEditing:(UITextField *)textField;
//当开始点击textField会调用的方法
2. -(void)textFieldDidEndEditing:(UITextField *)textField;
//当textField编辑结束时调用的方法
3. -(BOOL)textFieldShouldReturn:(UITextField *)textField{ //按下Done按钮的调用方法,我们让键盘消失
但本實作暫時只用到 textFieldShouldReturn,以下是實作。
1. 在TextFieldTestViewController.h加入delegate
#import <UIKit/UIKit.h>
@interface TextFieldTestViewController : UIViewController <UITextFieldDelegate>
{
UITextField *textInput;
}
@property (weak, nonatomic) IBOutlet UILabel *MyLabel;
@property (weak, nonatomic) IBOutlet UIButton *MyButton;
@property (weak, nonatomic) IBOutlet UITextField *MyTextField;
@end
2. 在TextFieldTestViewController.m加入程式碼
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setTextField];
}
- (IBAction)onBackgroundHit:(id)sender {
[MyTextField resignFirstResponder];
[textInput resignFirstResponder];
}
- (void) setTextField
{
textInput = [[UITextField alloc] init];
[textInput setFrame:CGRectMake(120, 50, 100, 50)];
textInput.borderStyle = UITextBorderStyleRoundedRect;
textInput.font = [UIFont systemFontOfSize:15];
textInput.placeholder = @"enter text";
textInput.autocorrectionType = UITextAutocorrectionTypeNo;
textInput.keyboardType = UIKeyboardTypeDefault;
textInput.returnKeyType = UIReturnKeyDone;
textInput.clearButtonMode = UITextFieldViewModeWhileEditing;
textInput.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textInput.delegate = self;
[self.view addSubview:textInput];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
MyLabel.text = textField.text;
return YES;
}
@end
3.結果圖
一般我們通常會用到三個method
1. -(void)textFieldDidBeginEditing:(UITextField *)textField;
//当开始点击textField会调用的方法
2. -(void)textFieldDidEndEditing:(UITextField *)textField;
//当textField编辑结束时调用的方法
3. -(BOOL)textFieldShouldReturn:(UITextField *)textField{ //按下Done按钮的调用方法,我们让键盘消失
但本實作暫時只用到 textFieldShouldReturn,以下是實作。
1. 在TextFieldTestViewController.h加入delegate
#import <UIKit/UIKit.h>
@interface TextFieldTestViewController : UIViewController <UITextFieldDelegate>
{
UITextField *textInput;
}
@property (weak, nonatomic) IBOutlet UILabel *MyLabel;
@property (weak, nonatomic) IBOutlet UIButton *MyButton;
@property (weak, nonatomic) IBOutlet UITextField *MyTextField;
@end
2. 在TextFieldTestViewController.m加入程式碼
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setTextField];
}
- (IBAction)onBackgroundHit:(id)sender {
[MyTextField resignFirstResponder];
[textInput resignFirstResponder];
}
- (void) setTextField
{
textInput = [[UITextField alloc] init];
[textInput setFrame:CGRectMake(120, 50, 100, 50)];
textInput.borderStyle = UITextBorderStyleRoundedRect;
textInput.font = [UIFont systemFontOfSize:15];
textInput.placeholder = @"enter text";
textInput.autocorrectionType = UITextAutocorrectionTypeNo;
textInput.keyboardType = UIKeyboardTypeDefault;
textInput.returnKeyType = UIReturnKeyDone;
textInput.clearButtonMode = UITextFieldViewModeWhileEditing;
textInput.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textInput.delegate = self;
[self.view addSubview:textInput];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
MyLabel.text = textField.text;
return YES;
}
@end
3.結果圖
2013年1月20日 星期日
UIToolBar的基本使用(五)
延續前一篇的實驗,改變ToolBar上 Button的文字。
實作上,直接在dynamicToolBarViewController.m上加入程式碼即可
-(void)showText1
{
NSString *text = @"測試一";
showLabel.text = text;
[self changeBarItemTitle:0];
}
-(void)showText2
{
NSString *text = @"測試二";
showLabel.text = text;
[self changeBarItemTitle:2];
}
-(void)showText3
{
NSString *text = @"測試三";
showLabel.text = text;
[self changeBarItemTitle:4];
}
- (void) changeBarItemTitle:(int) item_num
{
NSMutableArray *newItems = [myToolBar.items mutableCopy];
UIBarButtonItem *old = [newItems objectAtIndex:item_num];
[old setTitle:@"已經按過了"];
[newItems replaceObjectAtIndex:item_num withObject:old];
myToolBar.items = newItems;
}
結果顯示
實作上,直接在dynamicToolBarViewController.m上加入程式碼即可
-(void)showText1
{
NSString *text = @"測試一";
showLabel.text = text;
[self changeBarItemTitle:0];
}
-(void)showText2
{
NSString *text = @"測試二";
showLabel.text = text;
[self changeBarItemTitle:2];
}
-(void)showText3
{
NSString *text = @"測試三";
showLabel.text = text;
[self changeBarItemTitle:4];
}
- (void) changeBarItemTitle:(int) item_num
{
NSMutableArray *newItems = [myToolBar.items mutableCopy];
UIBarButtonItem *old = [newItems objectAtIndex:item_num];
[old setTitle:@"已經按過了"];
[newItems replaceObjectAtIndex:item_num withObject:old];
myToolBar.items = newItems;
}
結果顯示
訂閱:
文章 (Atom)


















