Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

UIButton.addTarget in ProMotion #345

Closed
redsparklabs opened this Issue · 7 comments

3 participants

@redsparklabs

I am porting an existing RubyMotion app over to ProMotion and for some reason have hit a roadblock on this piece.

@registerButton = UIButton.buttonWithType(UIButtonTypeRoundedRect)
@registerButton.frame = [[10, 65], [(self.view.frame.size.width  / 2) - 15, 40]]
@registerButton.setTitle('Register', forState: UIControlStateNormal)
@registerButton.addTarget(self,
                          action:'register',
                          forControlEvents:UIControlEventTouchUpInside)

Maybe it's because it's friday, but I can't wrap my head around this. :(

thx

@markrickert
Collaborator

Overall, the code looks correct. Typically you don't reference the view with self.view. Just use view.

What kind of error message are you getting so we can better troubleshoot?

@redsparklabs

I am trying to convert that block of code to the ProMotion style

add UIButton.new, {
  set_title: 'Register',forState:UIControlStateNormal,
  addTarget: self, action:'register',forControlEvents:UIControlEventTouchUpInside,
  frame: CGRectMake(10,165,(frame.size.width / 2) - 15, 20)
}

Not getting any error code, but nothing is showing up.

@markrickert
Collaborator

Hmm... I've never actually created a button like this before using the set_attributes helper (what add uses under the hood), but I'm sure that the properties are different than the original obj-c methods (i.e. setTitle:forState:).

I think you might be able to do something like this:

add UIButton.new, {
  "setTitle:forState:": ['Register', UIControlStateNormal],
  "addTarget:action:forControlEvents": [self, 'register', UIControlEventTouchUpInside],
  frame: CGRectMake(10,165,(frame.size.width / 2) - 15, 20)
}

but I'll defer to @jamonholmgren for confirmation.

@redsparklabs

that throws an error

expecting tASSOC
  "setTitle:forState:": ['Register', UIControlStateNormal],

I will try this

add UIButton.new, {
  "setTitle:forState:" => ['Register', UIControlStateNormal],
  "addTarget:action:forControlEvents" => [self, 'register', UIControlEventTouchUpInside],
  frame: CGRectMake(10,165,(frame.size.width / 2) - 15, 20)
}

I did find that this works.

@registerButton = add UIButton.buttonWithType(UIButtonTypeRoundedRect), {
  font: UIFont.systemFontOfSize(20),
  frame: [[10,165],[(self.frame.size.width / 2) - 15, 20]]
}
@registerButton.setTitle('Register', forState:UIControlStateNormal)
@registerButton.addTarget(self,action:'register', forControlEvents:UIControlEventTouchUpInside)
@redsparklabs

That's the ticket. This works exactly as intended.

add UIButton.buttonWithType(UIButtonTypeRoundedRect), {
  "setTitle:forState:" => ['Register', UIControlStateNormal],
  "addTarget:action:forControlEvents" => [self, 'register', UIControlEventTouchUpInside],
  frame: CGRectMake(10,165,(frame.size.width / 2) - 15, 20)
}

thanks for the help

@markrickert
Collaborator

Awesome! Glad you got it working!

@jamonholmgren

Nice! And don't listen to @markrickert, I use self.view all the time. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.