How to add a UIButton to GLKit View? - ios

I need to add a UIButton on a GLKit view. I am trying the same on the default openGL program that is created by Xcode.
Can some one help me out with this?

I finally figured this out myself. Posting detail just incase someone else is looking at the same problem.
To add a button to your GLKView, add the following code snippet in your ViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
//Initialise GLKView. Set Context, depth format etc.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 10, 10);
[button setTitle:#"Button" forState:UIControlStateNormal];
[button addTarget:self action:#selector(onClick:) forControlEvents:UIControlEventTouchDown];
[button setEnabled:YES];
[self.view addSubview:button];
}
-(void)onClick:(id)sender
{
//Do onClick stuff here
[self resignFirstResponder];
}
-
Hochester

Related

tvOS buttons created in code cannot be focused

Creating my first yellow world style tvOS app and cannot get my first button to work...
No matter what I cannot get this button to be focused. I've tried the same thing in swift with the same result.
- (void)viewDidLoad {
[super viewDidLoad];
startSlideshowButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 300, 50)];
[startButton setTitle:#"Start" forState:UIControlStateNormal];
[startButton addTarget:self action:#selector(startPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startButton];
[self setNeedsFocusUpdate];
}
-(UIView*)preferredFocusedView{
return startButton;
}
Was using the wrong controlEvent
correct:
[startButton addTarget:self action:#selector(startPressed:) forControlEvents:UIControlEventPrimaryActionTriggered];

How do I properly add a button to my scene using only code?

Working off of other Stack Overflow answers, I have managed to come up with the code below:
(in my homescreen.m file)
UIButton *GCButton;
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
//add button
[self addGameCenterButton];
}
return self;
}
-(void)addGameCenterButton
{
GCButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
[GCButton setBackgroundColor:[UIColor orangeColor]];
[GCButton setTitle: #"My Button" forState:UIControlStateNormal];
[GCButton setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
[GCButton.layer setBorderWidth:1.0f];
[GCButton.layer setBorderColor:[UIColor blueColor].CGColor];
//adding action programatically
[GCButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:GCButton];
}
-(void)buttonClicked:(UIButton*)sender
{
NSLog(#"you clicked on button %ld", (long)sender.tag);
}
As of right now, I see nothing on my screen. Is there anything else I need to add in my .m or .h files to make the button work?
From the comments of your question, I'm assuming you are using SpriteKit. In a GameScene, unfortunately, you cannot use UIButtons normally.
To create buttons for your scene, please check the answers in the link below:
Setting up buttons in SKScene

#Selector nothing happen

I Know this is probably very easy question, but i can't do that, i am using #selector to dismiss a view, writing this code, nothing happen:
- (void)viewDidLoad {
//here create my button
dismiss = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dismiss.frame = CGRectMake(100, 10, 60, 50);
[dismiss setTitle:#"Close" forState:UIControlStateNormal];
[dismiss setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[dismiss addTarget:self action:#selector(closeView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:dismiss];
}
//here the function
- (void)closeView {
[self dismissViewControllerAnimated:YES completion:nil];
}
But nothing happen, where am I doing mistake?
thanks
Try writing the addTarget line of code after [self.view addSubview:dismiss]; and check
That depends on what you're trying to do. dismissViewControllerAnimated:completion: will dismiss a controller that has been presented with presentViewController:animated:completion:, if there is no controller presented, it won't do anything.

ACTION for UIBUTTON in iOS

I have one custom class with subclass of UIView, inside this I created one button dynamically.
Now I want to set Action for button method. I set normal like UIviewController.
But its not working.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *but=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:but];
// Initialization code
}
return self;
}
-(void)aMethod
{
NextEyeViewController *nexteye=[[NextEyeViewController alloc]initWithNibName:#"NextEyeViewController" bundle:nil];
[self presentViewController:nexteye animated:YES completion:nil];
}
-(void)aMethod is my method name for button
If you want to set an action to your button created programmatically you need to do like this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 160.0, 40.0);
[view addSubview:button];
Create UIButton:
UIButton *yourButton = [[UIButton alloc] initWithFrame:frameButton];
// Here code to set button properties: color, label...
[yourButton addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:yourButton];
Your method has to receive the sender of the action:
-(void)aMethod:(id)sender {
// your code
}
Update:
You also have to call your method suitably with #selector(aMethod:) instead of #selector(aMethod).

IBAction & Buttons programmatically

I'm creating buttons programmatically and I then want to add functionality whereby when they are tapped on/pressed, they stay highlighted unless tapped on again. What I'm doing now is creating the buttons and then trying to add an IBAction. However, the issue is I have my button creation in a method and then I'm not sure how to reference the button in my IBAction. Here's my code:
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:#selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender{
//Not sure what to do here, since this method doesn't recognize testButn, How do I reference testButn
The sender is testButn. You should change stayPressed's argument type from (id) to (UIButton *)
Unless an action method is connected to several different classes of objects, it's best to replace the id with whatever object class you're using. This can be useful if you're hooking things up in IB, since it won't let you hook it to the wrong kind of object.
The fact that it's not working isn't because it doesn't recognize your button. Your approach is wrong. I think you need to have an action connected to touch down, and maybe set the selected state to YES. In your button definition, you need to set an imageForState: for the selected state. The way you're doing it now, that method isn't called until touch up.
Something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"New_PICT0019.jpg"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"New_PICT0002.jpg"] forState:UIControlStateSelected];
[testButn addTarget:self action:#selector(stayPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:testButn];
}
-(void)stayPressed:(UIButton *) sender {
if (sender.selected == YES) {
sender.selected = NO;
}else{
sender.selected = YES;
}
}
You need to cast sender to UIButton.
- (IBAction)staypressed:(id)sender
{
UIButton *theButton = (UIButton*)sender;
//do something to theButton
}
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:#selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
testButn.tag = 1;
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender
{
if ([sender tag]==1)
{
somecodes...
}
}

Resources