,我怎样才能解决这个问题?告诉我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)
}}
发布于 2014-12-01 17:15:01
只需将两个中的一个移动到另一个,除了这一行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)
}只是好奇,你为什么要声明两次这个方法?
https://stackoverflow.com/questions/27233586
复制相似问题