2016-10-17 2 views
-3

У меня есть два контроллера вида, ViewControllerA и ViewControllerB, где ViewControllerB является подклассом ViewControllerA. Вот реализация кода в ViewControllerA.Как добавить UIButton программно в ViewControllers

class ViewControllerA: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    createButton() 
    buttonPressed() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning()  
} 

func createButton() { 
    let button = UIButton(); 
    button.setTitle("Add", forState: .Normal) 
    button.setTitleColor(UIColor.blueColor(), forState: .Normal) 
    button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height 
    button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside) 
    self.view.addSubview(button) 
} 

func buttonPressed(sender: UIButton!) { 
    var alertView = UIAlertView(); 
    alertView.addButtonWithTitle("Done"); 
    alertView.title = "Alert!"; 
    alertView.message = "Button Pressed!!!"; 
    alertView.show(); 
} 

В моей ViewControllerB я хотел бы использовать CreateButton(), но не так, как он был использован в ViewControllerA. Предположим, что я хочу иметь красный цвет не синий в ViewControllerB в createButton(). Как я могу переопределить createButton(), чтобы быть отличным в ViewControllerB?

+0

Перевернув его - 'override func createButton()'. В чем проблема? – matt

+1

Одним из вариантов было бы добавить параметр цвета в метод 'createButton'. – rmaddy

+0

Мэтт, я понимаю, чтобы переопределить его, как вы сказали. Если я переопределю этот путь, я должен иметь такой же объем кода в ViewControllerB только для разницы цветов в методе createButton(). – BiHMaster

ответ

-1

следующее будет работать.

class ViewControllerA: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    createButton() 
    buttonPressed() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning()  
} 

func createButton() { 
    let button = UIButton(); 
    button.setTitle("Add", forState: .Normal) 
    button.setTitleColor(UIColor.blueColor(), forState: .Normal) 
    button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height 
    button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside) 
    self.view.addSubview(button) 
} 

func buttonPressed(sender: UIButton!) { 
    var alertView = UIAlertView(); 
    alertView.addButtonWithTitle("Done"); 
    alertView.title = "Alert!"; 
    alertView.message = "Button Pressed!!!"; 
    alertView.show(); 
} 


class ViewControllerB: ViewControllerA { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning()  
} 

override func createButton() { 
    let button = UIButton(); 
    button.setTitle("Add", forState: .Normal) 
    button.setTitleColor(UIColor.blueColor(), forState: .Normal) 
    button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height 
    button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside) 
    self.view.addSubview(button) 
} 

override func buttonPressed(sender: UIButton!) { 
    var alertView = UIAlertView(); 
    alertView.addButtonWithTitle("Done"); 
    alertView.title = "Alert!"; 
    alertView.message = "Button Pressed!!!"; 
    alertView.show(); 
} 

Надеемся, что это поможет тем, кто также пытается понять, как лучше подобрать подкласс контроллера.

Смежные вопросы