swift3 - Reusable *button* in Xcode -
i've created actionsheet 3 options push other view controllers , call number. option on button on navigation bar on each screen of app. i'm trying find way reuse code without literally re-typing every screen. understand there way through subclassing uibutton
i've searched days , can't find definitive answer on how code subclass. code right following:
class morebutton: uibutton { @ibaction func displayactionsheet(_ sender: any) { let alertcontroller = uialertcontroller(title: nil, message: "get roast on", preferredstyle: .actionsheet) let followaction = uialertaction(title: "follow us", style: .default, handler: {(action: uialertaction!)->void in self.performsegue(withidentifier: "movesegue", sender: self) }) let shopaction = uialertaction(title: "visit shop", style: .default, handler: {(action: uialertaction!)->void in self.performsegue(withidentifier: "shopsegue", sender: self) }) let callaction = uialertaction(title: "call now", style: .default) { _ in let url:url = url(string: "tel://number")! uiapplication.shared.open(url, options: [:], completionhandler: nil) } let cancel = uialertaction(title: "cancel", style: .cancel, handler: nil) alertcontroller.addaction(followaction) alertcontroller.addaction(shopaction) alertcontroller.addaction(callaction) alertcontroller.addaction(cancel) self.present(alertcontroller, animated: true, completion: nil) } }
i'm getting error:
value of type 'morebutton' has no member 'performsegue'
in both followaction , shopaction lines , "value of type 'morebutton' has no member 'present'" on last line. code working when had created ibaction
directly button seems have errors i'm not sure how correct.
you're getting errors because performsegue
, present
uiviewcontroller methods.
instead of subclassing uibutton, i'd create new helper class, class method build uialertcontroller you. method accept uiviewcontroller parameter, can build uialertactions correctly.
you'd call new method displayactionsheet
like:
let alertcontroller = actionsheetbuilder.build(viewcontroller: self) present(alert, animated: true, completion: nil)
adapting creation code new class come out like:
final class actionsheetbuilder { class func build(viewcontroller: uiviewcontroller) -> uialertcontroller { let alertcontroller = uialertcontroller(title: nil, message: "get roast on", preferredstyle: .actionsheet) let followaction = uialertaction(title: "follow us", style: .default, handler: {(action: uialertaction!)->void in viewcontroller.performsegue(withidentifier: "movesegue", sender: viewcontroller) }) let shopaction = uialertaction(title: "visit shop", style: .default, handler: {(action: uialertaction!)->void in viewcontroller.performsegue(withidentifier: "shopsegue", sender: viewcontroller) }) let callaction = uialertaction(title: "call now", style: .default) { _ in let url:url = url(string: "tel://2845471035")! uiapplication.shared.open(url, options: [:], completionhandler: nil) } let cancel = uialertaction(title: "cancel", style: .cancel, handler: nil) alertcontroller.addaction(followaction) alertcontroller.addaction(shopaction) alertcontroller.addaction(callaction) alertcontroller.addaction(cancel) return alertcontroller } }
Comments
Post a Comment