简体   繁体   中英

how can i show retrieved data from database in not a present view controller but another one in ios with using swift

i'm making database in main view controller and now i want to retrieve its stored data in to next view controller but i don't know, can any one give me answer of it.

a code block:

override func viewDidLoad()
{
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let filemgr = NSFileManager.defaultManager()
    let dirPaths =
        NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
                                            .UserDomainMask, true)

    let docsDir = dirPaths[0]
    let path = (docsDir as NSString).stringByAppendingPathComponent("contacts.db")


    databasePath = path

    if !filemgr.fileExistsAtPath(databasePath as String) {

        let contactDB = FMDatabase(path: databasePath as String)

        if contactDB == nil {
            print("Error: \(contactDB.lastErrorMessage())")
        }

        if contactDB.open() {
            let sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)"
            if !contactDB.executeStatements(sql_stmt) {
                print("Error: \(contactDB.lastErrorMessage())")
            }
            contactDB.close()
        } else {
            print("Error: \(contactDB.lastErrorMessage())")
        }
    }

}

using this code i made database but how can i retrieve its data in next view controller again i'm repeating not on a same page another page i want to display this.

Do not implement your database in a view controller but in your app delegate.

Example:

// declare global database
var myGlobalDB:FMDatabase?

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // do your database init stuff here
    ...
    myGlobalDB = FMDatabase(path: databasePath as String)
    ...
}

Your myGlobalDB will be available from anywhere.

While what Michael said is correct if you want to push data to the next ViewController use a segue.

If you create a variable to hold the data in the SecondViewController:

var database : FMDatabase?

and in your prepareForSegue :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "YourIdentifier" {
        if let vc = segue.destinationViewController as? SecondViewController {
            vc.database = self.contactDB
        }
    }
}

Change as? SecondViewController as? SecondViewController to the classname of your Second View Controller and change "your identifier" to the segue identifier you use to switch to the Second View Controller. If only 1 segue is used than it isn't required but it is good practice to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM