2015-04-02 149 views
1

我有「屏幕A」和「屏幕B」,我有「屏幕A」上的按鈕,可以導航到「屏幕B」,如何從另一個屏幕快速更改一個屏幕的顏色?

在「屏幕B」上,我有兩個按鈕「按鈕紅色」和「按鈕綠色」,我想通過按「屏幕B」

按鈕來改變「畫面A」的顏色我想做到這一點使用委託

即使解決方案在Objective-C也有利於

+0

你最終搞清楚了嗎? – 2015-04-02 19:54:15

回答

2

你可以做到這一點通過以下方式:

protocol ViewControllerBDelegate: class { 
    func changeColor(color : UIColor) 
} 

class ViewControllerB: UIViewController { 

    weak var delegate : ViewControllerBDelegate? 

    @IBAction func changeColorInViewController(sender: UIButton) { 
     // send the message to change the color in A regarding the color 
     sender.tag == 0 ? delegate?.changeColor(UIColor.redColor()) : 
         delegate?.changeColor(UIColor.greenColor()) 
    } 
} 

以上ViewController是要更改ViewControllerA的顏色ViewControllerB

然後你就可以實現以下方式ViewControllerA

class ViewControllerA: UIViewController , ViewControllerBDelegate { 

    var viewControllerB : ViewControllerB! 

    // In this method you receive the notification when the button in B is tapped 
    func changeColor(color: UIColor) { 
     self.view.backgroundColor = color 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {   
     var dest = segue.destinationViewController as! ViewControllerB 

     self.viewControllerB = dest // instantiate the reference 
     self.viewControllerB.delegate = self // set the delegate for B 
    } 
} 

兩個重要的事情:

  1. 我設置在prepareForSegueViewControllerB參考,因爲你有一個按鈕,打開ViewControllerB,但在您手動呈現它時,您可以根據需要更改它。
  2. 我只爲ViewControllerB中的兩個按鈕實現了action,並且我爲每個按鈕分配了一個tag(您可以在Interface Builder或代碼中執行此操作)來識別它併發送有關按鈕按下的顏色,但可以如果你想分開做。

我希望這對你有所幫助。

+0

你會希望委託財產弱,以避免保留週期。我會建議對此進行編輯。很多人都沒有在Swift中出於某種原因這樣做,但仍然應該如此。 – 2015-04-02 15:35:45

+0

@ pasta12是的,你是對的,我會編輯我的代碼 – 2015-04-02 15:37:17

+0

好的或接受我的編輯。 – 2015-04-02 15:37:33

0

我能想到的最簡單的方法

都說屏幕A是FirstViewController,它已加入腳本名稱「屏幕A」

var storyboard = UIStoryboard(name: "Main", bundle: nil) 
var controller: UIViewController = storyboard.instantiateViewControllerWithIdentifier("Screen A") as FirstViewController 
controller.view.backroundColor = UIColor.redColor() 
+0

謝謝,bur我想用代表 – sss 2015-04-02 14:11:49

3

我不會拼出來的代碼你,因爲委託模式是我認爲你真的需要出去工作你自己,所以它是有道理的,但我會給你你需要的要求。

  1. 請在屏幕B.協議這將包含您的委託方法
  2. 添加一個委託方法傳回的顏色,屏幕A.命名約定將類似於- (void)shouldUpdateToColor:(UIColor *)color
  3. 添加弱,非原子id<YourProtocol>類型的財產調用委託(或任何你想稱之爲)。
  4. 在屏幕A,宣告畫面A會從B屏
  5. 符合YourProtocol實現屏幕的委託方法的
  6. 設置屏幕A作爲B屏的代表,當你轉換到B屏
  7. 按下按鈕時,在屏幕B中調用委託方法。由於屏幕A是代表,它將調用屏幕A中的方法,它將更新您的按鈕。

Here's a link about delegation in Swift

+0

加上它聽起來像一個家庭作業。爲什麼任何一個正確思考的人不只是訪問視圖對象並改變其顏色? – 2015-04-02 14:14:58

+0

謝謝,真的很好的答案我會嘗試使用你自己的方式來做到這一點,是的,就像它瞭解代表 – sss 2015-04-02 14:15:09

+0

我假設它是兩個單獨的視圖控制器,而不僅僅是兩個視圖。 – 2015-04-02 14:16:13

1

目標C:

@protocol ScreenBDelegate <NSObject> 

- (void)screenBChangedColor:(UIColor *)color; 

@end 

@interface ScreenB : UIViewController 

@property (weak, nonatomic) id<ScreenBDelegate>delegate; 

@end 

@implementation ScreenB 

- (IBAction)buttonRedTapped 
{ 
    if([self.delegate respondsToSelector:@(screenBChangedColor:)]){ 
     [self.delegate screenBChangedColor:[UIColor redColor]]; 
    } 
} 

@end 

@interface ScreenA() < ScreenBDelegate> 

@end 

@implementation ScreenA 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.screenB.delegate = self; //find the best place to do it 
} 

- (void)screenBChangedColor:(UIColor *)color 
{ 
    self.view.backGroudColor = color; 
} 

@end 
+0

謝謝..尼斯幫助 – sss 2015-04-02 14:21:08

+0

所以批准它:) – Klevison 2015-04-02 14:23:54

相關問題