I have a iOS5 project using storyboard and with ARC on.
I have a view in storyboard with the class thisViewController and in there I got a smaller subview which I dragged in and gave it the class thisView.
thisView has a custom drawRect function, that works and draws what I want nicely.
But I also want to add buttons dynamically, so I'll add them in the initWithFrame method of thisView like so:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"This is called <3");
UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 50, 50);
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
[self bringSubviewToFront:btn]; // last thing I tried, didn't work, thought z-index was to low or something
}
return self;
}
The function is being called since the NSLog displays, but the button is nowhere to be found
--EDIT--
Extra information:
ThisView.h
#interface RadarView : UIView <CLLocationManagerDelegate>{
CLLocation *currentLocation;
}
Dont add the SubView in the init method.
Add the SubView in the - (void)layoutSubviews.
- (void)layoutSubviews {
[super layoutSubviews];
if (![self viewWithTag:12345]) {
UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 50, 50);
btn.tag = 12345;
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
}
I found what the problem was thanks to some comments and the chat
There was an init in the radarviewcontroller, which triggered initwithframe, which made the NSLog display, and the initwithcoder wasn't used so I didn't see anything, that was the problem and got stuff mixed up. Thanks for commenting and helpin!
Related
I making some simple AR app with interfaces using overlaySKScene.
In viewDidLoad, I applied overlay scene in main ARSCNView(OverlayScene is my class inherited SKScene).
self.sceneView.overlaySKScene = [[OverlayScene alloc] initWithSize:self.sceneView.bounds.size];
self.sceneView.overlaySKScene.delegate = self;
Then, I added UIkit button in OverlayScene init.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[button setTitle:#"button" forState:UIControlStateNormal];
button.layer.position = CGPointMake(self.view.frame.size.width/2, 200);
[self.view addSubview:button];
But, that button didn't appear on screen.
When I added sprite node in this scene, it worked.
Is there other method for it or bug?
Thank you.
OverlayScene's init isn't really an appropriate place to do that, as self.view will equal nil because when a scene is initialize it has yet to be added to the view hierarchy. A better choice might be the SKScene lifecycle method - (void)didMoveToView:(SKView *)view; like:
- (void)didMoveToView:(SKView *)view {
[super didMoveToView:view];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[button setTitle:#"button" forState:UIControlStateNormal];
button.layer.position = CGPointMake(self.view.frame.size.width/2, 200);
[self.view addSubview:button];
}
- (void)viewDidLoad{
int leftBorder = 80;
int topBorder = 160;
int width = 150;
int height = 50;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(leftBorder, topBorder, width, height)];
myView.layer.cornerRadius = 5;
myView.backgroundColor = [UIColor redColor];
[self.view addSubview:myView];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testButton.frame = CGRectMake(0, 0, 50, 50);
[testButton setTitle:#"testButton" forState:UIControlStateNormal];
[testButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myView addSubview:self.testButton];
self.myView.hidden = YES;
[super viewDidLoad];
}
Hi, sorry for stupid question! I'm newby in xcode. Why I didn't see this button? And How can I hide button after click? I need button inside the frame.
Simple remove self.myView.hidden = YES;
To add you click listener, two solution:
By code in your viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[mybutton addTarget:self action:#selector(myButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
}
- (void)myButtonClick:(id)sender {
myButton.hidden = YES;
}
Or via interface Builder (preferred), The easiest way is to actually define the handler/action in Xcode using the IBAction declaration in the interface file (add the declaration just before the #end statement). Then attach the action to the button
you are adding self.testButton instead of the created testButton.
[self.myView addSubview:testButton];
you did not assign myView to your property.
[self.view addSubview:myView]; self.myView = myView;
remove self.myView.hidden = YES;
Another remark:
You should usually call super as early as possible. Otherwise, the superclass may interfere with your own implementation.
There are several issues in your code.
You add testButton as subview to self.myView. Then you hide self.myView. As a result neither self.myview nor its subviews will be visible.
You get confused with your local varialbles and instance varialbes. Apparently there is an instance variable and property myView. Otherwise you coudn't use self.myView. And you declare a local variable myView, which is not the same as the instance variable. This may well be perfectly ok. But I got a guts feeling that you did this not intentionally. Your self.myView may well be nil at the point in time where you add the subview. And even the subview self.testButton may be nil. That will compile file and execute fine but nothing does happen actually.
The same for testButton.
I suggest to change the code a bit, assuming there are properties for myView and testButton of the appropriate types:
self.myView = [[UIView alloc] initWithFrame:CGRectMake(leftBorder, topBorder, width, height)];
self.myView.layer.cornerRadius = 5;
self.myView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.myView];
self.testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.testButton.frame = CGRectMake(0, 0, 50, 50);
[self.testButton setTitle:#"testButton" forState:UIControlStateNormal];
[self.testButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myView addSubview:self.testButton];
self.myView.hidden = NO;
I am working on the basics of iOS development.
So far I have a button that on press, will show some text. But what I want to do is after it is pressed, I want it to then change the text of the button, so far this is what I have:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.backgroundColor = [UIColor redColor];
button.titleLabel.textColor=[UIColor blackColor];
button.frame = CGRectMake(25, 100, 275, 60);
[button setTitle:#"Press this button to reveal the text!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button_method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)button_method:(UIButton *)sender {
NSString *test = #"I am learning Objective-C for the very first time! Also, this is my first ever variable!";
// handle button press
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = test;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
When I try adding [button setTitle:#"You pressed the button"]; to button_method
This doesn't work... why? And how would I make it work?
When I try adding [button setTitle:#"You pressed the button"]; to button_method, it doesn't work... why?
Because that method is nonexistent on UIButton.
And how would I make it work?
By using a method that UIButton actually responds to. For example:
[sender setTitle:#"You pressed the button" forState:UIControlStateNormal];
And read the relevant documentation, please.
Your code will not change the title of the button. But simply add a label as a subview for your view. Is this what you want?
If you want to change the text on the button you will want to do sender.title.text = test; in your button_method
I also recommend adding a NSLog(#"Button pressed"); to your button press method to double check that it is being called.
Since starting with iPhone app development (last 9 months) I have only used IB. I have a project to work on already built by another developer that I need to optimise for iPhone screen. No problem in IB, I know how to do that, in this project however the Nav bar is added using code only and is an image view. Could someone advise me how I go about resizing/positioning the nav bar when IB isnt used? Im trying to enhance this app for the iphone 5 screen.
#define BAR_FRAME CGRectMake(0,0,320.0f,43.0f)
#implementation ICNavbarView
#synthesize homeButton=__homeButton;
#synthesize prevButton=__prevButton;
#synthesize nextButton=__nextButton;
#synthesize delegate=__delegate;
- (id)initWithFrame:(CGRect)frame
{
LogCmd();
self = [super initWithFrame:BAR_FRAME];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.alpha = 0.9f;
// Add Navigation bar background // <<<<<< navigation bar from ui image
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
// Add back button
__prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
__prevButton.frame = CGRectMake(30.0f, 6.0f, 29.0f, 31.0f);
UIImage *prevButtonPressed = [UIImage imageNamed:#"BtnPrevPressed"];
[__prevButton setImage:[UIImage imageNamed:#"BtnPrev"] forState:UIControlStateNormal];
[__prevButton setImage:prevButtonPressed forState:UIControlStateSelected];
[__prevButton setImage:prevButtonPressed forState:UIControlStateHighlighted];
[__prevButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__prevButton];
// Add next button
__nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
__nextButton.frame = CGRectMake(262.0f, 6.0f, 29.0f, 31.0f);
UIImage *nextButtonPressed = [UIImage imageNamed:#"BtnNextPressed"];
[__nextButton setImage:[UIImage imageNamed:#"BtnNext"] forState:UIControlStateNormal];
[__nextButton setImage:nextButtonPressed forState:UIControlStateSelected];
[__nextButton setImage:nextButtonPressed forState:UIControlStateHighlighted];
[__nextButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__nextButton];
// Add home button
__homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
__homeButton.frame = CGRectMake(145.0f, 6.0f, 31.0f, 30.0f);
UIImage *homeButtonPressed = [UIImage imageNamed:#"BtnHomePressed"];
[__homeButton setImage:[UIImage imageNamed:#"BtnHome"] forState:UIControlStateNormal];
[__homeButton setImage:homeButtonPressed forState:UIControlStateSelected];
[__homeButton setImage:homeButtonPressed forState:UIControlStateHighlighted];
[__homeButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__homeButton];
}
return self;
}
- (id)init
{
return [self initWithFrame:CGRectZero];
}
#pragma mark - Button handlers
- (void)buttonPressed:(id)sender
{
if (sender == __prevButton) {
[self.delegate performSelector:#selector(navBarPrevButtonPressed)];
} else if (sender == __homeButton) {
[self.delegate performSelector:#selector(navBarHomeButtonPressed)];
} else {
[self.delegate performSelector:#selector(navBarNextButtonPressed)];
}
}
#end
So far I tried UIViewAutoresizingFlexibleHeight; like this
// Add Navigation bar background
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
//resize
bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
This seems to make no difference and the nav bar hasn't moved
I've used this before, but I used the setter function instead of using the property. I don't know if that would change anything or not, but try it this way:
[bgView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
The parens are important, if you have multiple re-sizing options.
im doing a test for subclassing uiControl, but it is not working yet,
here my code:
MainVC.m::
- (void)viewDidLoad
{
[super viewDidLoad];
TesteaContol *tt = [[[TesteaContol alloc]initWithFrame:CGRectMake(20, 20, 80, 30)]autorelease];
[tt addTarget:self action:#selector(ttActiono:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tt];
}
- (void)ttActiono:(TesteaContol*)message
{
NSLog(#"proyection");
}
TesteaContol.h
#interface TesteaContol : UIControl
TesteaControl.m
#import "TesteaContol.h"
#implementation TesteaContol
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"cao");
UIButton *vacas = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[vacas setTitle:#"pingus" forState:UIControlStateNormal]; ;
vacas.titleLabel.textColor = [UIColor blackColor];
vacas.frame = CGRectMake(0, 0, 80, 40);
[vacas addTarget:self action:#selector(vacasPressed:) forControlEvents:UIControlStateNormal];
[self addSubview:vacas];
}
return self;
}
- (void) vacasPressed:(id) sender
{
NSLog(#"vacanio");
[self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
so I am testing with a uiButton, i see the button, but is not responding to the touches,
what im i missing?
thanks!
There is a problem here:
[vacas addTarget:self action:#selector(vacasPressed:)
forControlEvents:UIControlStateNormal];
The "forControlEvents" part should be of type UIControlEvents which UIControlStateNormal is not. For a regular button, you want to replace UIControlStateNormal with UIControlEventTouchUpInside:
[vacas addTarget:self action:#selector(vacasPressed:)
forControlEvents:UIControlEventTouchUpInside];