我是iOS和编程菜鸟,所以我为任何不好的措辞或错误道歉。
我正在为我的应用程序的API解析引号,每次单击UIButton时,它都会在textLabel上显示它。为了防止字符串脱离textLabel或调整为不可读的字体,如果字符串字符数太高,我试图通过调用我的NSObject中的函数来请求新的引号。我设置了一个NSObject进行重新获取,但每当我尝试将字符串重新分配给NSObject的textLabel.text或尝试将字符串发送回ViewController时,qouteLabel.text将返回nil
这是我的viewcontroller,我在这里提出了报价的初始请求
import UIKit
import Alamofire
class RSQuotesViewController: RSViewController {
var ronImageView: UIImageView!
var quoteLabel = UILabel!()
override func loadView() {
let frame = UIScreen.mainScreen().bounds
let view = UIView(frame: frame)
view.backgroundColor = UIColor.grayColor()
ronImageView = UIImageView(frame: CGRectMake(frame.width/2-160, frame.height-600, 320, 600))
let ron = "ron.png"
let ronImage = UIImage(named: ron)
ronImageView.image = ronImage
view.addSubview(ronImageView);
let labelWidth = ronImageView.frame.width/2
let quoteLabelX = labelWidth-40
quoteLabel = UILabel(frame: CGRect(x: quoteLabelX, y: ronImageView.frame.height/4+15, width: labelWidth, height: 160))
quoteLabel.textAlignment = .Center
quoteLabel.text = "Click to Start"
quoteLabel.shadowColor = UIColor.grayColor()
quoteLabel.adjustsFontSizeToFitWidth = true
quoteLabel.lineBreakMode = .ByWordWrapping // or NSLineBreakMode.ByWordWrapping
quoteLabel.numberOfLines = 0
view.addSubview(quoteLabel)
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
let frame = UIScreen.mainScreen().bounds
let getQuote = UIButton(frame: CGRect(x: 0, y: 0, width: frame.size.width+50, height: frame.size.height))
getQuote.backgroundColor = UIColor.clearColor()
getQuote.setTitle("", forState: UIControlState.Normal)
getQuote.addTarget(self, action: #selector(RSQuotesViewController.getQuote(_:)), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(getQuote)
}
// Gets quote when button is pressed
func getQuote(sender: UIButton){
let url = "http://ron-swanson-quotes.herokuapp.com/v2/quotes"
Alamofire.request(.GET, url, parameters: nil).responseJSON { response in
if let JSON = response.result.value as? Array<String>{
let quoteDict = RSQoute()
// if quote is too large get another one
if (JSON[0].characters.count > 120){
print("greater than 120")
quoteDict.fetchQuote()
} else {
self.quoteLabel.text = JSON[0]
}
}
}
}
这是我的模型,我试图重新分配quoteLabel.text并获得nil
import UIKit
import Alamofire
class RSQoute: NSObject {
var newQuote = String()
// fetchs new quote if quote is too large
func fetchQuote(){
let url = "http://ron-swanson-quotes.herokuapp.com/v2/quotes"
Alamofire.request(.GET, url, parameters: nil).responseJSON { response in
if let JSON = response.result.value as? Array<String>{
self.newQuote = JSON[0]
if (self.newQuote.characters.count > 120) {
print("Try Again: ---->\(self.newQuote)")
return self.fetchQuote()
} else {
let quoteVC = RSQuotesViewController()
print("Retry was less than 120: ---->\(self.newQuote)")
print("quoteLabelText: ---->\(RSQuotesViewController().quoteLabel.text)")// comes back nil
RSQuotesViewController().quoteLabel.text = self.newQuote
}
}
}
}
}
如果我遗漏了某些内容或者尝试从API获取新报价的更简单/更好的方法,请告诉我们。)
答案 0 :(得分:1)
在函数fetchQuote()
中,将quoteVC设置为带有let quoteVC = RSQuotesViewController()
的RSQuotesViewController()的新实例。相反,您应该为quoteLabel.text
的应用程序实例设置RSQuotesViewController()
。您还提出了两个API请求。进入fetchQuote()
的{{1}}功能,进入RSQuotesViewController
的{{1}}功能
我认为你所寻找的将涉及闭包。试试fetchQuote()
类
RSQuotes
函数
fetchQuote()
然后,我会有一个RSQuotes
函数 func fetchQuote(completion: (result:String)){
let url = "http://ron-swanson-quotes.herokuapp.com/v2/quotes"
Alamofire.request(.GET, url, parameters: nil).responseJSON { response in
if let JSON = response.result.value as? Array<String>{
self.newQuote = JSON[0]
if (self.newQuote.characters.count > 120) {
print("Try Again: ---->\(self.newQuote)")
completion(result: self.newQuote)
} else {
print("Retry was less than 120: ---->\(self.newQuote)")
print("quoteLabelText: ---->\(RSQuotesViewController().quoteLabel.text)")// comes back nil
completion(result: self.newQuote)
}
}
,你可以做这样的事情
setQuote
我会看看一些与快速关闭有关的帖子,也可以查看。 http://goshdarnclosuresyntax.com/
另外,我不确定您是否打算在RSQuotesViewController
课程中操纵quoteString。如果不是,func setQuote() {
let quoteObj = RSQuote()
quoteObj.fetchQuote() {
result in
quoteLabel.text = result
}
}
可能更适合成为RSQuote
。这样您就可以在不fetchQuote()
初始化对象的情况下调用它。这就像static func