Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 3    Views: 44

sarahk
Code is Art
Profile
Posts: 159
Reg: Jul 16, 2014
Auckland
10,290
08/31/14 10:10 PM (9 years ago)

Clicking a button runs a method/event

Typically a BT button click opens a screen or launches something. What is the right way to say "do this function which will record this click somewhere and change the image to a tick"? Kind of like Angry Ninja does in Instant database when someone records that they've been to a restaurant.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
09/01/14 12:53 AM (9 years ago)
In iOS it's pretty simple. 1) create a button in your 'interface' (.h) file: UIButton *btnMyButton; 2) synthesize it in your implementation (.m) file: @synthesize btnMyButton; 3) either include code for, or create a separate method for creating the button: - (void)loadbtnMyButton { // btnMyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btnMyButton setTitle:@"My Button Title" forState:UIControlStateNormal]; [btnMyButton setFrame:CGRectMake(140, 5, 120, 40)]; // this is both size and screen position coordinates [btnMyButton addTarget:self action:@selector(myCustomMethod) forControlEvents:UIControlEventTouchUpInside]; [[self view] addSubview: btnMyButton]; // } (you would call this in viewWillAppear ([self loadbtnMyButton];), or similar method. or, just use the code within in one of the 'willappear/load' methods) 4) create a method to handle the button click, seen in the 'selector' method of the loadbtnMyButton method: -(void) myCustomMethod { // do something in code when the button clicks } This is how it is done 'in code'. using interface builder is a little easier, but you have to create xib files for each screen size. It's a personal preference... in Android: 1) create a button within your layout xml file <Button android:id="@+id/btnMyButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/terms_button" /> 2) in a method, declare your button Button btnMyButton = (Button)screenView.findViewById(R.id. btnMyButton); 3) and create an 'on click listener' to detect button clicks: btnMyButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { BT_debugger.showIt(fragmentName + ": btnMyButton just got clicked!"); } }); It seems a bit odd out of context, but if you nab a copy of my 'smug msg loc' (a free plugin) you can see examples in use for both Android and iOS. hope this helps! Cheers! -- Smug Edit: and to add to your actual 'question', in the button click event, you would tell the 'object' to change to whatever you want it to be. For instance, in iOS there is no 'checkbox', so in my SmugEula, I created a button that uses two images; check 'on' and check 'off'. When the button is tapped, it goes to a method that handles the button action and appearance: -(void)checkTouched { [BT_debugger showIt:self theMessage:@"checkTouched"]; if (sweulaCheckButton.imageView.image == [UIImage imageNamed:@"sw_eula_cb1.png"]) { [BT_debugger showIt:self theMessage:@"checkTouched - 0"]; [sweulaCheckButton setImage:[UIImage imageNamed:@"sw_eula_cb0.png"] forState:UIControlStateNormal]; } else { [BT_debugger showIt:self theMessage:@"checkTouched - 1"]; [sweulaCheckButton setImage:[UIImage imageNamed:@"sw_eula_cb1.png"] forState:UIControlStateNormal]; } }
 
sarahk
Code is Art
Profile
Posts: 159
Reg: Jul 16, 2014
Auckland
10,290
like
09/01/14 02:02 PM (9 years ago)
Thanks Smug, awesome as always!
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
09/01/14 03:29 PM (9 years ago)
Sweet writeup, Smug! Thank you, Mr. general Manager :-) -- Niraj
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.