2013年1月23日 星期三

iOS -- Segue 傳入

使用 SEGUE 輸出與輸入資料

e.g. RootViewController 是一TableViewController,將Cell的值傳入DetailViewController。然後,在DetailVC 的TextView編輯好的資料,在回傳入RootViewController上頭的Cell,改變Cell 的值.


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
RootViewContrller.h 裡要設一 NSDictionary *returnSelection; 來接收回傳的值
RootViewController.m:


//傳資料到DetailController 
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    UIViewController *destination = segue.destinationViewController;//segue的目的控制器指定給destination
    
    //第一個if是回傳時用的
    if ([destination respondsToSelector:@selector(setDelegate:)]) { //如果對setDelegate:方法有反應,便將自己存成value,keydelegate
        [destination setValue:self forKey:@"delegate"];
    }
    
    //第二個if是傳出用的
    if ([destination respondsToSelector:@selector(setMySelection:)]) {//如果對setSelection:方法有反應,請準備處理選擇後的值
        
        //prepare selection info
        
        NSIndexPath *indexPath = [self.table indexPathForCell:sender];//將選擇的行列指定給indexPath
        id object = [self.arr objectAtIndex:indexPath.row];//取出入徑索引的物件,指定予object
        NSDictionary *mySelection = [NSDictionary dictionaryWithObjectsAndKeys:indexPath, @"indexPath", object, @"object", nil]; //將其入徑索引和物件存入到一個dictionary
        [destination setValue:mySelection forKey:@"mySelection"];//設定下一個控制器的KVC
    }
}


//Setter: 取得來自DetailController的回傳delegate
- (void) setReturnSelection:(NSDictionary *)dict {
    
    if (![dict isEqual:returnSelection]) {
        
        returnSelection = dict;
        NSIndexPath *indexPath = [dict objectForKey:@"indexPath"];
        id newValue = [dict objectForKey:@"object"];
        [tasks replaceObjectAtIndex:indexPath.row withObject:newValue];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}


+++++++++++++++++++++++++++++++++++++++
DetailViewContoller.h 要設 id delegate;
DetailViewController.m

//接收root傳來的值
- (void)viewDidLoad
{
    [super viewDidLoad];
NSString *voc = [mySelection objectForKey:@"object"];
}


//回傳到root:

- (void) viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    if ([delegate respondsToSelector:@selector(setReturnSelection:)]) {
        
        //finish editing
        [textView endEditing:YES];
        //prepare selection info
        NSIndexPath *indexPath = [mySelection objectForKey:@"indexPath"];//回傳到正確的Cell Row
        id object = textView.text;
        NSDictionary *returnSelection = [NSDictionary dictionaryWithObjectsAndKeys:indexPath, @"indexPath", object, @"object", nil];
        
        
        [delegate setValue:returnSelection forKey:@"returnSelection"];
        
    }
}


*******************************************************
********************************************************
Xcode 的樣板

這裡的 data 只是單存的 array:



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        
       NSDate *object = [_objects objectAtIndex:indexPath.row];
        
        [[segue destinationViewController] setDetailItem:object];
    }
}

iOS - Keyboard resigns

iOS  離開textField,按背景時,鍵盤降下 Keyboard resigns




//Did End On Exit  輸入完畢時鍵盤降下

- (IBAction)textFieldReturn:(id)sender { //這裡的sender應該是keyboard
    
    [sender resignFirstResponder];
}


//Touch Down 按背景時,鍵盤降下
- (IBAction)backgroundTouched:(id)sender { //這裡的sender應該是背景
    
    [_tempTextField resignFirstResponder];
}


iOS6 - Seque btw 2 View Controllers

iOS6 使用 Segue 傳輸兩個View Controller 之間的字串資料


Storyboard的設定:



在Scene1 的 TextField 輸入字串 "I love you." 按下按鈕,資料將傳到 Scene 2 的 TextField. 如下圖:

 然後在Scene2編輯後再回傳資料到Scene1,如下:

    
  


分別為這兩個ViewControllers建立兩個Classes,如下:
--------------------------------------------------------------
SceneOneViewController.h: 

#import <UIKit/UIKit.h>

@interface SceneOneViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *SceneOneTextField;
@property (strong, nonatomic) NSString *textOne;

@end


___________________________________________
SceneOneViewController.m: 


#import "SceneOneViewController.h"
#import "SceneTwoViewController.h"

@interface SceneOneViewController ()

@end

@implementation SceneOneViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

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


//Pass data in scene one to text in scene 2
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    SceneTwoViewController *destination = [segue destinationViewController];
    destination.text = _SceneOneTextField.text;
    
}

//Receive data from scene 2 and display in scene 1; the value of var "textOne" should be set in scene 2.m
- (IBAction)returned:(UIStoryboardSegue *)segue {

    _SceneOneTextField.text = _textOne;
}

@end


注意:在Storyboard的Scene2 Controller 裡,將按鈕連到下方綠色的小方格(Exit),選擇上述的 IBAction: returned。讓它離開Scene2回到Scene1時,要執行這個動作,如下:






________________________________________________
SceneTwoViewController.h: 


#import <UIKit/UIKit.h>

@interface SceneTwoViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *SceneTwoTextField;
@property (strong, nonatomic) NSString *text;

@end
________________________________________________
SceneTwoViewController.m: 


#import "SceneTwoViewController.h"
#import "SceneOneViewController.h"

@interface SceneTwoViewController ()

@end

@implementation SceneTwoViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
_SceneTwoTextField.text = _text;
}

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


//Pass the data from scene 2 to textOne
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    SceneOneViewController *destinationVC = [segue destinationViewController];
    destinationVC.textOne = _SceneTwoTextField.text;
    
}

@end