2017-07-18 2 views
-3

以下は私のコードです。私は、背景画像を変更する私のビューに9つのボタンを持っています。私を助けてください。外部メソッドからuibuttonメソッドにアクセスするのがintrestedですが、多くのエラーが発生します

import UIKit 

class ViewController: UIViewController { 
    let alerts = UIAlertController(title: "Hi", message: "Hello", preferredStyle: UIAlertControllerStyle.alert) 

    let action1 = UIAlertAction(title: "Yes", style: UIAlertActionStyle.default) { (<#UIAlertAction#>) in 
     for i in 0..<9 
     { 
      let newButton = self.view.viewWithTag(i) as! UIButton 
      newButton.setBackgroundImage(UIImage.init(named: ""), for: .normal) 
     } 
    } 

    @IBAction func buttonPressed(_ sender: UIButton) 
    { 
     sender.setBackgroundImage(UIImage.init(named: "cross.png"), for:.normal) 
    } 
} 
+0

? – Venkat

+0

エラーは何ですか?どのラインが原因ですか?これらの詳細であなたの質問を編集してください。 – rmaddy

+0

私は蛇口コードのボタンにアクセスしようとしています.. newButton = self.view.viewWithTag(i)を! UIButton newButton.setBackgroundImage(UIImage.init(名前: "")、for:.normal) –

答えて

0

これを試してください:あなたはUIAlertControllerにアクションを追加する必要があります

newButton .setBackgroundImage(UIImage.init(named: ""), for: .normal) 
newButton .setBackgroundImage(UIImage.init(named: "cross.png"), for: .selected) 

@IBAction func buttonPressed(_ sender: UIButton) 
    { 
     sender.isSelected = !sender.isSelected; 
    } 
+0

私は見つけることができますか?IB関数以外の関数から新しいボタン変数を指す方法(特定のタグを使ってストーリーボードに既に作成されています) –

+0

currentSymbol == 1の場合 { (のための: "cross.png"):UIImage(名前UIControlState()) センダ.setBackgroundImage currentStateの[sender.tag] = 1 currentSymbol = 2 }そうcurrentSymbol == 2 {送信側場合。 setBackgroundImage(UIImage(名前: "nought.png")、for:UIControlState()) currentState [sender.tag] = 2 currentSymbol = 1 } –

+0

これは、クリックすると背景画像を変更するために使用しているコードです。 –

0

。アクション1を定義した後、次の行を追加します。

[alerts addAction: action1] 
0

はこれを試してみてください:

最初のビューにボタンを追加し、そのためのアクションを追加します。

for i in 0..<9 
    { 
      let newButton = UIButton(frame : yourFrame) 
      newButton.setBackgroundImage(UIImage.init(named: ""), for: .normal) 
      newButton.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside) 
      newbutton.tag = i 
      self.view.addSubview(newButton:) 
    } 
あなたがそれらの9のボタンを追加している
関連する問題