ios - Call method in another View Controller, but not segue to it? -


i have tabbed application.

tab : download button.

tab b : tableview

what want when click button in a, want download function in b called.

now have several scenarios after click button:

  • stay in a
  • if click b now, can see download process, or if finished, can see file there.

    +----------------+         +-----------------+ |                |         |downloaded files | |                |         +-----------------+ | +------------+ |         |                 | | | download   | |         +-----------------+ | +------------+ |         |                 | |                |         |                 | +-------+--------+         +--------+--------+ |now in |   b    |         |      | in | |     |        |         |        |    b   | +-------+--------+         +--------+--------+ 

now write download function in a, , save files in directory, whenever click tab b, viewwillappear function load downloaded files directory, problem want show download process in case file large.

any suggestion , code snippet appreciated.

using swift 3

before provide solution i'll point @paulw11's comment, he's 100% correct. proper convention pull connectivity logic out of view controllers. code should have in view controllers code necessary display view. connecting server , managing result has nothing presenting resulting data in view.

if felt compelled call function directly view, you shouldn't it creating new instance of view controller. rather, should existing instance this:

let app = uiapplication.shared.delegate! as! appdelegate     if let viewcontrollers = app.window?.rootviewcontroller?.childviewcontrollers {         viewcontrollers.foreach { vc in             if let view = vc as? anotherviewcontroller {                 view.downloadit()             }         }     } 

personally, i'd recommend pulling logic out of view controller either way. if you're trying keep things minimal, or if web service call making, this:

add new swift file protocol , extension protocol:

protocol downloadable : class { }  extension downloadable {      func downloadstuff() -> string {        // add download logic here        return "downloading"     }  } 

then, in view want able use download function add protocol view controller this:

class viewcontroller: uiviewcontroller, downloadable {} 

then can downloaded data this:

@ibaction func handledownloadtapped(_ sender: uibutton) {     let result = downloadstuff()     print(result) } 

again, shouldn't in habit of doing can messy if working few different api calls.

the best practice create class contains web service stuff, objc.io approach. can find tutorial here.

https://talk.objc.io/episodes/s01e01-networking

basically you'll create service this

public typealias jsondict = [string: anyobject]  struct resource<a> {     let url: url     let parse: (nsdata) -> a? }  let url = url(string: "https://url")!  let myresource = resource<[jsondict]>(url: url) { data in     let json = try? jsonserialization.jsonobject(with: data data, options: [])     return json as? [jsondict] }  final class connect {     class func load<a>(resource: resource<a>, completion: @escaping (a?) -> void) {         urlsession.shared.datatask(with: resource.url) { (data, _, _ ) in              if let data = data {                 completion(resource.parse(data nsdata))             } else {                 completion(nil)             }             }.resume()     } } 

and can call wherever need this:

connect.load(resource: myresource) { result in     // stuff result } 

i hope helpful someone


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -