Ios 如何在swift 3的另一个视图控制器中隐藏UIActivityIndicatorView?
我在第二个视图控制器中有一个Ios 如何在swift 3的另一个视图控制器中隐藏UIActivityIndicatorView?,ios,swift3,uiviewcontroller,uiactivityindicatorview,Ios,Swift3,Uiviewcontroller,Uiactivityindicatorview,我在第二个视图控制器中有一个UIActivityIndicatorView,当用户在第一个视图中选择按钮时,Json将启动,但同时应用程序将运行第二个viewcontroller,使用UIActivityIndicatorView为第一个视图中的Json设置动画viewcontroller已在第二个视图控制器中完成停止UIActivityIndicatorView(只需在另一个viewcontroller中停止并隐藏UIActivityIndicatorView) 以下是第一个视图控制器的代码
UIActivityIndicatorView
,当用户在第一个视图中选择按钮时,Json
将启动,但同时应用程序将运行第二个viewcontroller
,使用UIActivityIndicatorView
为第一个视图中的Json
设置动画viewcontroller
已在第二个视图控制器中完成停止UIActivityIndicatorView
(只需在另一个viewcontroller
中停止并隐藏UIActivityIndicatorView
)
以下是第一个视图控制器的代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
selectedCell.backgroundColor = UIColor.clear
selectedCell.tintColor = UIColor.clear
print("Chat Subject Has Benn Selected!")
let selectedItem = indexPath.row
print(selectedItem)
chatViewController.selectedChat = selectedItem
///////////////////////Start Chat
print(EmailSignInViewController.id)
////////////////////// Get chat Page
let urlPath3: String = "http://example/api?api_token=\(EmailSignInViewController.api_token)&id=\(EmailSignInViewController.id[selectedItem])"
print(urlPath3)
let url3: NSURL = NSURL(string: urlPath3)!
let request3: NSMutableURLRequest = NSMutableURLRequest(url: url3 as URL)
request3.httpMethod = "GET"
let queue3:OperationQueue = OperationQueue()
NSURLConnection.sendAsynchronousRequest(request3 as URLRequest, queue: OperationQueue.main) {(response, data, error) in
do {
let data = try Data(contentsOf: url3 as URL)
print(NSString(data: data, encoding: String.Encoding.utf8.rawValue)!)
} catch {
print(error.localizedDescription)
}
do {
if let jsonResult3 = try JSONSerialization.jsonObject(with: data!, options: []) as? Array<Any> {
for item in jsonResult3 {
print("item")
print(item)
if let dict = item as? NSDictionary {
if let type = dict.value(forKey: "type"){
chatViewController.type.append(type as! Int)
print("type")
print("\([type])")
chattingViewController.checklist.append(type as! Int)
}
if let text = dict.value(forKey: "text"){
chatViewController.text.append(text as!String)
chattingViewController.list.append(text as!String)
print("text")
print("\([text])")
}
if let created_at = dict.value(forKey: "created_at"){
self.created.append(created_at as!String)
print("created_at")
print("\([created_at])")
print("\([created_at])")
}
}
}
print(jsonResult3)
print("ASynchronous\(jsonResult3)")
chatViewController.created_at = self.created
print("chat Page")
}
} catch let error as NSError {
print(error.localizedDescription)
}
self.performSegue(withIdentifier: "chatting", sender: nil)
}
更新::真正的问题是,在导航发生后,您正在将
UIActivityIndicator
添加到第二个视图控制器的viewDidLoad()
方法中
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.startAnimating() //Add here
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
selectedCell.backgroundColor = UIColor.clear
selectedCell.tintColor = UIColor.clear
print("Chat Subject Has Benn Selected!")
let selectedItem = indexPath.row
print(selectedItem)
chatViewController.selectedChat = selectedItem
///////////////////////Start Chat
print(EmailSignInViewController.id)
////////////////////// Get chat Page
let urlPath3: String = "http://example/api?api_token=\(EmailSignInViewController.api_token)&id=\(EmailSignInViewController.id[selectedItem])"
print(urlPath3)
let url3: NSURL = NSURL(string: urlPath3)!
let request3: NSMutableURLRequest = NSMutableURLRequest(url: url3 as URL)
request3.httpMethod = "GET"
let queue3:OperationQueue = OperationQueue()
NSURLConnection.sendAsynchronousRequest(request3 as URLRequest, queue: OperationQueue.main) {(response, data, error) in
do {
let data = try Data(contentsOf: url3 as URL)
print(NSString(data: data, encoding: String.Encoding.utf8.rawValue)!)
} catch {
print(error.localizedDescription)
}
do {
if let jsonResult3 = try JSONSerialization.jsonObject(with: data!, options: []) as? Array<Any> {
for item in jsonResult3 {
print("item")
print(item)
if let dict = item as? NSDictionary {
if let type = dict.value(forKey: "type"){
chatViewController.type.append(type as! Int)
print("type")
print("\([type])")
chattingViewController.checklist.append(type as! Int)
}
if let text = dict.value(forKey: "text"){
chatViewController.text.append(text as!String)
chattingViewController.list.append(text as!String)
print("text")
print("\([text])")
}
if let created_at = dict.value(forKey: "created_at"){
self.created.append(created_at as!String)
print("created_at")
print("\([created_at])")
print("\([created_at])")
}
}
}
print(jsonResult3)
print("ASynchronous\(jsonResult3)")
chatViewController.created_at = self.created
print("chat Page")
}
} catch let error as NSError {
print(error.localizedDescription)
}
self.activityIndicator.stopAnimating() //Removed here
self.performSegue(withIdentifier: "chatting", sender: nil)
}
更新::真正的问题是,在导航发生后,您正在将
UIActivityIndicator
添加到第二个视图控制器的viewDidLoad()
方法中
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.startAnimating() //Add here
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath as IndexPath)!
selectedCell.backgroundColor = UIColor.clear
selectedCell.tintColor = UIColor.clear
print("Chat Subject Has Benn Selected!")
let selectedItem = indexPath.row
print(selectedItem)
chatViewController.selectedChat = selectedItem
///////////////////////Start Chat
print(EmailSignInViewController.id)
////////////////////// Get chat Page
let urlPath3: String = "http://example/api?api_token=\(EmailSignInViewController.api_token)&id=\(EmailSignInViewController.id[selectedItem])"
print(urlPath3)
let url3: NSURL = NSURL(string: urlPath3)!
let request3: NSMutableURLRequest = NSMutableURLRequest(url: url3 as URL)
request3.httpMethod = "GET"
let queue3:OperationQueue = OperationQueue()
NSURLConnection.sendAsynchronousRequest(request3 as URLRequest, queue: OperationQueue.main) {(response, data, error) in
do {
let data = try Data(contentsOf: url3 as URL)
print(NSString(data: data, encoding: String.Encoding.utf8.rawValue)!)
} catch {
print(error.localizedDescription)
}
do {
if let jsonResult3 = try JSONSerialization.jsonObject(with: data!, options: []) as? Array<Any> {
for item in jsonResult3 {
print("item")
print(item)
if let dict = item as? NSDictionary {
if let type = dict.value(forKey: "type"){
chatViewController.type.append(type as! Int)
print("type")
print("\([type])")
chattingViewController.checklist.append(type as! Int)
}
if let text = dict.value(forKey: "text"){
chatViewController.text.append(text as!String)
chattingViewController.list.append(text as!String)
print("text")
print("\([text])")
}
if let created_at = dict.value(forKey: "created_at"){
self.created.append(created_at as!String)
print("created_at")
print("\([created_at])")
print("\([created_at])")
}
}
}
print(jsonResult3)
print("ASynchronous\(jsonResult3)")
chatViewController.created_at = self.created
print("chat Page")
}
} catch let error as NSError {
print(error.localizedDescription)
}
self.activityIndicator.stopAnimating() //Removed here
self.performSegue(withIdentifier: "chatting", sender: nil)
}
如果要将第二个viewController作为子对象添加到第一个viewController,则可以通过[child.activityIndicator stop animating]关闭第二个viewController的UIActivityIndicator视图;我该怎么做?请帮助我请添加一些代码以更好地帮助您。好的,我添加了第一个视图控制器的代码。如果您将第二个视图控制器作为子视图添加到第一个视图控制器,则可以通过[child.activityIndicator stop animating]关闭第二个视图控制器的UIActivityIndicator视图;我该怎么做?请帮助我请添加一些代码以帮助您更好。好的,我添加了第一视图控制器的代码看起来像许多聊天应用程序,如viber或whatsapp或。。。我想当用户选择聊天开始时,因为速度非常重要,之后当Json完成UIActivityIndicatorView时,停止并隐藏这只是建议遵循最佳实践,但是您可以通过上面添加的代码关闭activityIndicator,但是如果您要将activityIndicator添加到parentView,那么请使用self.activityIndicator.stopAnimating()确定,那么如何添加它?只需将故事板中的内容拖动到第一个视图控制器代码,还是不拖动?只需添加以下行chatViewController.activityIndicator.stopAnimating()或self.activityIndicator.stopAnimating()根据ActivityIndicator的父视图,我的第二个视图控制器名为chattingViewController,第一个视图控制器名为ChatViewController,与viber、whatsapp或。。。我想当用户选择聊天开始时,因为速度非常重要,之后当Json完成UIActivityIndicatorView时,停止并隐藏这只是建议遵循最佳实践,但是您可以通过上面添加的代码关闭activityIndicator,但是如果您要将activityIndicator添加到parentView,那么请使用self.activityIndicator.stopAnimating()确定,那么如何添加它?只需将故事板中的内容拖动到第一个视图控制器代码,或者不拖动?您只需添加以下行chatViewController.activityIndicator.stopAnimating()或self.activityIndicator.stopAnimating(),具体取决于activityIndicator的父视图查看我的第二个视图控制器chattingViewController,第一个是chatViewController
chattingWaiting.startAnimating()
chattingWaiting.isHidden = false