2014-12-01 89 views
0

我該如何解決這個問題?告訴我,viewDidLoad被宣佈2次,但我可以統一整個過程?覆蓋func viewDidLoad

import UIKit 
import MessageUI 
import CoreData 
import MediaPlayer 
import AVFoundation 

class ViewController: UIViewController, UISearchBarDelegate, MFMailComposeViewControllerDelegate, MPMediaPickerControllerDelegate, AVAudioPlayerDelegate { 
     override func viewDidLoad() { 
     super.viewDidLoad() 

     title = "Media picker..." 

     buttonPickAndPlay = UIButton.buttonWithType(.System) as? UIButton 

     if let pickAndPlay = buttonPickAndPlay{ 
      pickAndPlay.frame = CGRect(x: 0, y: 0, width: 200, height: 37) 
      pickAndPlay.center = CGPoint(x: view.center.x, y: view.center.y - 50) 
      pickAndPlay.setTitle("Pick and Play", forState: .Normal) 
      pickAndPlay.addTarget(self, 
       action: "displayMediaPickerAndPlayItem", 
       forControlEvents: .TouchUpInside) 
      view.addSubview(pickAndPlay) 
     } 

     buttonStopPlaying = UIButton.buttonWithType(.System) as? UIButton 

     if let stopPlaying = buttonStopPlaying{ 
      stopPlaying.frame = CGRect(x: 0, y: 0, width: 200, height: 37) 
      stopPlaying.center = CGPoint(x: view.center.x, y: view.center.y + 50) 
      stopPlaying.setTitle("Stop Playing", forState: .Normal) 
      stopPlaying.addTarget(self, 
       action: "stopPlayingAudio", 
       forControlEvents: .TouchUpInside) 
      view.addSubview(stopPlaying) 
     } 
    } 

    @IBOutlet weak var Subject: UITextField! 


    @IBOutlet weak var Body: UITextView! 

    @IBOutlet weak var searchBar: UISearchBar! 

    @IBOutlet weak var webView: UIWebView! 


    override func viewDidLoad() { 
     super.viewDidLoad() 


     searchBar?.delegate = self 

     let url = NSURL(string: "http://www.google.com") 
     let request = NSURLRequest(URL: url!) 

     webView?.scalesPageToFit = true 
     webView?.loadRequest(request) 

    } 

}

回答

0

一個兩個的身體剛剛進入另外一個,除了這行super.viewDidLoad(),必須調用一次:

override func viewDidLoad() { 
    super.viewDidLoad() 

    title = "Media picker..." 

    buttonPickAndPlay = UIButton.buttonWithType(.System) as? UIButton 

    if let pickAndPlay = buttonPickAndPlay{ 
     pickAndPlay.frame = CGRect(x: 0, y: 0, width: 200, height: 37) 
     pickAndPlay.center = CGPoint(x: view.center.x, y: view.center.y - 50) 
     pickAndPlay.setTitle("Pick and Play", forState: .Normal) 
     pickAndPlay.addTarget(self, 
      action: "displayMediaPickerAndPlayItem", 
      forControlEvents: .TouchUpInside) 
     view.addSubview(pickAndPlay) 
    } 

    buttonStopPlaying = UIButton.buttonWithType(.System) as? UIButton 

    if let stopPlaying = buttonStopPlaying{ 
     stopPlaying.frame = CGRect(x: 0, y: 0, width: 200, height: 37) 
     stopPlaying.center = CGPoint(x: view.center.x, y: view.center.y + 50) 
     stopPlaying.setTitle("Stop Playing", forState: .Normal) 
     stopPlaying.addTarget(self, 
      action: "stopPlayingAudio", 
      forControlEvents: .TouchUpInside) 
     view.addSubview(stopPlaying) 
    } 

    /// 

    searchBar?.delegate = self 

    let url = NSURL(string: "http://www.google.com") 
    let request = NSURLRequest(URL: url!) 

    webView?.scalesPageToFit = true 
    webView?.loadRequest(request) 
} 

只是珍玩,爲什麼你是否宣佈這種方法兩次?

+1

完美!很棒!謝謝Antonio!我宣佈這種方法兩次,因爲我是一個新手!他認爲,兩個按鈕(buttonPickAndPlay和buttonStopPlaying)應該只在viewController3中,而我在我所有的viewController中都能找到它們!我必須解決這個問題!感謝您的幫助和快速的關注! – Fabrizio 2014-12-01 18:05:17