2012年9月15日 星期六

全域變數的使用

一個在Xcode上使用全域變數的範例,本例從前一篇文字檔讀取方法修改而來,將讀取到資料當作全域變數來傳遞。此處是利用Delegate來作全域變數的傳遞。

1.新增一個UIViewController到storyBoard,並在此加上一個TextView及Button,原來的ViewController也加上一個Button來控制轉換Page的移動

2.從Page2的Button拉一個Segue到新的UIViewController

3. 新增一個Class檔來對應新的UIViewController


4. 修改新的UIViewController的 Class來對應到page2ViewController檔案

5. 註冊 storyBoard的元件到page2ViewController.h

6.在fileReadAppDelegate.h加上全域變數

#import <UIKit/UIKit.h>

@interface fileReadAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic, retain)   NSMutableArray *globalData;

@end

7. 在fileReadAppDelegate.m加上全域變數的設定
@implementation fileReadAppDelegate

@synthesize globalData;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

8.在fileReadViewController.h加上Delegate

#import <UIKit/UIKit.h>
#import "fileReadAppDelegate.h"

@interface fileReadViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextView *textDisplay;
@property (weak, nonatomic) IBOutlet UIButton *readButton;

@property (nonatomic, retain) fileReadAppDelegate *appDelegate;

@end


9. 在fileReadViewController.m加上Delegate的設定
#import "fileReadViewController.h"

@interface fileReadViewController ()

@end

@implementation fileReadViewController
@synthesize textDisplay;
@synthesize readButton;

@synthesize appDelegate;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
  
    appDelegate = (fileReadAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.globalData = [[NSMutableArray alloc] init];

}

- (void)viewDidUnload
{
    [self setTextDisplay:nil];
    [self setReadButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)readFileAction:(id)sender {
   
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
    if (filePath) {
        NSString *contentOfFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        textDisplay.text = contentOfFile;
       
        [appDelegate.globalData addObject:contentOfFile];
    }
   
    UIFont *font = [UIFont fontWithName:@"BiauKai" size:32];
   
    [textDisplay setFont:[UIFont fontWithName:@"BiauKai" size:32]];
}

@end

10. 在page2ViewController.h增加Delegate的變數
#import <UIKit/UIKit.h>
#import "fileReadAppDelegate.h"

@interface page2ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *textDisplay2;
@property (weak, nonatomic) IBOutlet UIButton *showButton;

@property (nonatomic, retain) fileReadAppDelegate *appDelegate;

@end

11. 在page2ViewController.m增加Delegate的設定,及全域變數的顯示設定。此處的IBAction要從storyBoard拉過來做聯結。

#import "page2ViewController.h"

@interface page2ViewController ()

@end

@implementation page2ViewController
@synthesize textDisplay2;
@synthesize showButton;

@synthesize appDelegate;

- (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.
}

- (void)viewDidUnload
{
    [self setTextDisplay2:nil];
    [self setShowButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)showResult:(id)sender {
    appDelegate = (fileReadAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *data =    [appDelegate.globalData objectAtIndex:0];
   
    textDisplay2.text = data;
   
    [textDisplay2 setFont:[UIFont fontWithName:@"BiauKai" size:28]];
}

@end

12.顯示結果如下,前後頁的字體大小稍稍不一樣。



沒有留言: