swift - Difference between before and after in viewDidLoad -
i have tried set searchbar
tableheaderview
inside of viewdidload
:
override func viewdidload() { super.viewdidload() // searchcontroller initializiation self.searchcontroller = uisearchcontroller.init(searchresultscontroller: nil) self.searchcontroller.delegate = self self.searchcontroller.searchbar.delegate = self self.searchcontroller.searchbar.sizetofit() self.searchcontroller.searchresultsupdater = self self.searchcontroller.searchbar.bartintcolor = uicolor.white self.searchcontroller.searchbar.keyboardappearance = .default self.searchcontroller.searchbar.backgroundcolor = uicolor.white self.searchcontroller.hidesnavigationbarduringpresentation = true self.searchcontroller.obscuresbackgroundduringpresentation = false self.tableview.tableheaderview = self.searchcontroller.searchbar self.definespresentationcontext = true self.fetch() self.tableview.reloaddata() }
and fetch()
function:
func fetch() { let fetchrequest:nsfetchrequest<phone> = phone.fetchrequest() fetchrequest.sortdescriptors = [nssortdescriptor.init(key: "header", ascending: true), nssortdescriptor.init(key: "date", ascending: true)] self.fetchedresultscontroller = nsfetchedresultscontroller.init(fetchrequest: fetchrequest, managedobjectcontext: self.managedobjectcontext, sectionnamekeypath: "header", cachename: nil) self.fetchedresultscontroller.delegate = self { try self.fetchedresultscontroller.performfetch() self.tableview.reloaddata() } catch {} }
but don't understand doesn't work. xcode crashes , nothing happens. , have tried change inside of viewdidload
method:
override func viewdidload() { super.viewdidload() self.fetch() self.tableview.reloaddata() // searchcontroller initializiation self.searchcontroller = uisearchcontroller.init(searchresultscontroller: nil) self.searchcontroller.delegate = self self.searchcontroller.searchbar.delegate = self self.searchcontroller.searchbar.sizetofit() self.searchcontroller.searchresultsupdater = self self.searchcontroller.searchbar.bartintcolor = uicolor.white self.searchcontroller.searchbar.keyboardappearance = .default self.searchcontroller.searchbar.backgroundcolor = uicolor.white self.searchcontroller.hidesnavigationbarduringpresentation = true self.searchcontroller.obscuresbackgroundduringpresentation = false self.tableview.tableheaderview = self.searchcontroller.searchbar self.definespresentationcontext = true }
success! works properly. don't understand difference?
it because set delegates searchresultsupdater , searchbar before fetchedresultscontroller initialised. in code searchresultsupdater or searchbar's delegate methods accessing fetchedresultscontroller , getting nil app crashes. suggestion avoid crash make searchresultscontroller computed or lazy property.
Comments
Post a Comment