ios - Calling a function from another class within a function swift -


i created class , function button:

class floatingactionbutton: uibuttonx {  override func begintracking(_ touch: uitouch, event: uievent?) -> bool {     uiview.animate(withduration: 0.3, animations: {         if self.maptypebutton.transform == .identity {               self.maptypebutton.transform = cgaffinetransform(rotationangle: 45 * (.pi / 180)) .                                } else {             self.maptypebutton.transform = .identity     return super.begintracking(touch, with: event)} 

now i'm in class viewcontroller , want call function (refer update why want call function)

func searchbarshouldbeginediting(_ searchbar: uisearchbar) -> bool {     self.closemaptype()     floatingactionbutton().begintracking(touch: uitouch, with: uievent?)     return true } 

but have been told begintracking not function type can called.

update:

i set button on storyboard , connected pointer. right now, button situated this (photo 1), (i apologize don't have enough reputation points post photo yet). when tap in simulator, changes this (photo 2), , when tap again, changes default state. of functionality set in floatingactionbutton class.

it works put uiview.animate(etc.) inside func searchbarshouldbeginediting i'd know if can call uiview.animate(etc.) reduce redundancy.

what want make when tap searchbar, if button in rotated state (photo 2), rotate original state (photo 1). i've created:

@iboutlet weak var maptypebutton: floatingactionbutton! func searchbarshouldbeginediting(_ searchbar: uisearchbar) -> bool {      uiview.animate(withduration: 0.3, animations: {         if self.maptypebutton.transform == .identity {             return         } else {             self.maptypebutton.transform = .identity     })     return true }  

and i'm looking for. rather calling function, added function actions new function. help

it sounds don't understand object-oriented programming.

a button object should not exist in vacuum. button user interface element, , belongs on-screen somewhere.

this code:

floatingactionbutton().begintracking(touch: uitouch, with: uievent?) 

creates brand-new floatingactionbutton, tells begin tracking touches, , forgets it. never has ties anything. nobody owns it, when searchbarshouldbeginediting function returns, button object gets deallocated.

is button supposed displayed on-screen somewhere, , track touches? if so, need decide view should host it, , add view. need set autolayout constraints gets placed in it's parent view.

in general, it's easier (and better) create user interface in storyboards, , place them in view hierarchy way. that, though, need explain how button fits user interface.

edit:

based on updates, want change settings on button in storyboard. (note call you're trying on button wrong, nevertheless i'll walk thorough how pointer (an iboutlet) button or other view can configure it.)

the way set storyboard in main editor window, , open source file view controller contains button in "assistant editor." suggest selecting "assistant editor on bottom" option this.

now hold down control key, press down on button, , control-drag button inside of view controller class, @ top near other variables. xcode offer create iboutlet button. in resulting dialog, change type of "sender" button type (a "floatingactionbutton".) if xcode won't let that, see if can select "uibutton" instead. give name "myfloatingbutton".

now change code from

floatingactionbutton().begintracking(touch: uitouch, with: uievent?) 

to

myfloatingbutton.begintracking(touch: uitouch, with: uievent?) 

what you've done create instance variable myfloatingbutton in view controller. when os loads view controller's views, save pointer floating button in instance variable. once view has loaded (once viewdidload() method called) can assume variable myfloatingbutton points button, , can call method configure it.

edit #2:

as hasan pointed out in answer, doesn't make sense call begintracking(_:with:) on button. method of uibutton (actually of button's ancestor class, uicontrol) system calls internally when user touches button.

you'll have explain you're trying in order able it.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -