Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
yea, I am just trying to understand the fundamentals for actually drawing stuff to the screen using pre-written methods in conjunction with openGL.

I am trying to get a button to draw to the screen, and while the program does compile and launch the simulator, I merely get a blank app running typical of Xcode templates.

the AppDelegate Class have an ivar and property of
Code:
UILabel *titleLabel

Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
  //...
	
	UIButton *button                  = [UIButton buttonWithType: UIButtonTypeRoundedRect];
	button.titleLabel.font            = [UIFont systemFontOfSize: 12];
	button.titleLabel.lineBreakMode   = UILineBreakModeTailTruncation;
	button.titleLabel.shadowOffset    = CGSizeMake (1.0, 0.0);
	
	
	[self.window addSubview: button];
	
}
 
yea, I am just trying to understand the fundamentals for actually drawing stuff to the screen using pre-written methods in conjunction with openGL.
None of the code you provided there deals with OpenGL. And, at this point of your education, you should probably ignore it for now.

TwinofSian said:
I am trying to get a button to draw to the screen, and while the program does compile and launch the simulator, I merely get a blank app running typical of Xcode templates.

Two things:
1) I really think it would be better if you set up a viewController and put code like this into it.
2) Your button has no frame. Therefore, it won't appear.
 
I thought the decleration:

Code:
UIButton *button                  = [UIButton buttonWithType: UIButtonTypeRoundedRect];

took care of that sort of thing or do I also need to try something like this:

Code:
CGRect frame = CGRectMake(0.0f, 0.0f, 100.0f, 30.0f);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;

Thanks for the help. I dont know if its showing, but I am beginning to get more and more the hang of this.
 
I thought the decleration:

Code:
UIButton *button                  = [UIButton buttonWithType: UIButtonTypeRoundedRect];

took care of that sort of thing or do I also need to try something like this:

Code:
CGRect frame = CGRectMake(0.0f, 0.0f, 100.0f, 30.0f);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;

Thanks for the help. I dont know if its showing, but I am beginning to get more and more the hang of this.

Your first line creates a button, but doesn't give it a frame. (I suspect it will have an empty frame rect to begin with, and so won't draw.)

Your second example uses a different type (custom) which doesn't draw anything by default.

In both examples, you're not adding the button to the parent view, so it won't show up anyway.

In previous code, you show adding buttons directly to the widow. In general, you add your root view controller's view to the window in your setup code in the app delegate, and then never change the window's contents again. You should not be adding views to the window.

If you're looking at examples/books that have you adding content directly to the window without a view controller, you need a newer book. That approach isn't really supported any more.

If you want to use OpenGL, you might want to take a look at GLKit. The new GLKView lets your treat an OpenGL view like a regular view, with our without a managing GLKViewController. It does most of the confusing, painful setup code for you. Beware, though, that OpenGL is one of the hardest APIs in iOS. It is big, complex, full of difficult concepts, low-level, and requires a lot of fussy setup to get working. If you're just getting started, it's a very bad choice for learning.
 
I thought the decleration:

Code:
UIButton *button                  = [UIButton buttonWithType: UIButtonTypeRoundedRect];

took care of that sort of thing...

Thinking and knowing are different things. In programming, you want to assume very, very little. If you don't know, find out. That is part of the process.

And, as I said to you previously, if you plan on replacing IB's work with your own code then you need to learn what it is that IB has been doing for you. You've now learned (I hope) that it sets a frame for your UIButton. What else is it doing?
 
Ok, so I have been asking how to draw this button. I suggested a method in combo with a data type (CGRect) but no one has actually posited the 'correct' methodology in as much as simply just drawing a button.

I've been scrounging the Apple dev site for days, so unless there's some magic to quickly finding what method to actually draw a button, I'd appreciate a little more indepth focus on the direct problem at hand. (although I do appreciate the conversation in general)
 
Ok, so I have been asking how to draw this button. I suggested a method in combo with a data type (CGRect) but no one has actually posited the 'correct' methodology in as much as simply just drawing a button.

You've already provided the code to 'simply' draw a button:
Code:
[[I]xxx[/I] addSubview: button];

But, I hope you are starting to realize that it is not so simple. There are other things you need to set up for your button before it appears. If you just want some code you can copy-and-paste, I'm afraid many of us around here will be less than willing to simply give you the answer.
 
I get that heh.


It's just that I don't get why I am having unsuccessful draws when I am able to compile just fine. There is clearly a disconnect in what I am doing and the right way.

I have a
Code:
AppDelegate.m
[self.window addSubview: button];
declaration but I still have no result. Could it be that the button is hidden UNDER the background layer??
 
Ok, so I have been asking how to draw this button. I suggested a method in combo with a data type (CGRect) but no one has actually posited the 'correct' methodology in as much as simply just drawing a button.

I've been scrounging the Apple dev site for days, so unless there's some magic to quickly finding what method to actually draw a button, I'd appreciate a little more indepth focus on the direct problem at hand. (although I do appreciate the conversation in general)

It's fairly likely that the way you are trying to add your button to the screen (adding it directly to the window) is preventing you from seeing the button at all. As I said, you should not be dealing with your app's window at all. The template code from Xcode takes care of adding a root view controller and connecting it to the window, and you should rarely, if ever, change the contents of the window directly.

Create your button (make it one of the standard button types like rounded rect) set it's frame, give it a title, and add it as a subview of your view controller's content view or any other view that's in that view hierarchy. That's all it should take. If you make your button type custom, it won't draw anything for you. You'd have to subclass your button and do the drawing yourself, or at least set a background image and/or title.
 
I've been scrounging the Apple dev site for days, so unless there's some magic to quickly finding what method to actually draw a button, I'd appreciate a little more indepth focus on the direct problem at hand. (although I do appreciate the conversation in general)

I have twice provided links to the UICatalog sample code project.

Have you looked at it? Have you studied exactly what it does to add a simple button?

Have you compiled and tested UICatalog, and confirmed it works as-is (no changes)? If it works as-is, which of the components it shows is closest to what you want to happen? Find the code for that, and study it. Then modify the code and see what happens. Modify it one small step at a time, so you can see if it breaks.


I'm a little confused about exactly what you want. You keep asking for a method to "draw a button". The thing is, unless you're making a completely custom button class, you don't usually have to write code that draws a button. You simply make a button, configure it properly, add it to a view, and it will then draw itself as and when drawing is needed. You never actually "draw a button"; you add it to a view and it draws itself (if it's been properly configured).

Getting the make, configure, and add parts correct is a major accomplishment by itself. That's why you should study the UICatalog example, because it shows how to do those things with all the standard UI components.

If you can't get a plain ordinary UIButton into a view and drawing correctly, there's no way you're going to get a customized draw-something-differently subclass of UIButton into a view and drawing correctly.
 
I have twice provided links to the UICatalog sample code project.

Have you looked at it? Have you studied exactly what it does to add a simple button?

Have you compiled and tested UICatalog, and confirmed it works as-is (no changes)? If it works as-is, which of the components it shows is closest to what you want to happen? Find the code for that, and study it. Then modify the code and see what happens. Modify it one small step at a time, so you can see if it breaks.


I'm a little confused about exactly what you want. You keep asking for a method to "draw a button". The thing is, unless you're making a completely custom button class, you don't usually have to write code that draws a button. You simply make a button, configure it properly, add it to a view, and it will then draw itself as and when drawing is needed. You never actually "draw a button"; you add it to a view and it draws itself (if it's been properly configured).

Getting the make, configure, and add parts correct is a major accomplishment by itself. That's why you should study the UICatalog example, because it shows how to do those things with all the standard UI components.

If you can't get a plain ordinary UIButton into a view and drawing correctly, there's no way you're going to get a customized draw-something-differently subclass of UIButton into a view and drawing correctly.


Very well said!
 
I have twice provided links to the UICatalog sample code project.

Have you looked at it? Have you studied exactly what it does to add a simple button?

Have you compiled and tested UICatalog, and confirmed it works as-is (no changes)? If it works as-is, which of the components it shows is closest to what you want to happen? Find the code for that, and study it. Then modify the code and see what happens. Modify it one small step at a time, so you can see if it breaks.

With respect, I have. I downloaded it. When I tried to compile it and run it, it broke. Gave me 44 errors telling me I was trying to store various data in things not structures or unions.


I'm a little confused about exactly what you want. You keep asking for a method to "draw a button". The thing is, unless you're making a completely custom button class, you don't usually have to write code that draws a button. You simply make a button, configure it properly, add it to a view, and it will then draw itself as and when drawing is needed. You never actually "draw a button"; you add it to a view and it draws itself (if it's been properly configured).

Fair enough. Looking at the apple developer site, the code I have written should be all I need, but I still am not getting a button drawn on the screen.

Getting the make, configure, and add parts correct is a major accomplishment by itself. That's why you should study the UICatalog example, because it shows how to do those things with all the standard UI components.
Agreed, but the UICatalog refuses to compile, and I don't want to figure out what the hell could be causing 44 errors--it's possibly meant for a later version of Xcode than I have.
 
With respect, I have. I downloaded it. When I tried to compile it and run it, it broke. Gave me 44 errors telling me I was trying to store various data in things not structures or unions.




Fair enough. Looking at the apple developer site, the code I have written should be all I need, but I still am not getting a button drawn on the screen.

Agreed, but the UICatalog refuses to compile, and I don't want to figure out what the hell could be causing 44 errors--it's possibly meant for a later version of Xcode than I have.

What version of Xcode are you using? I first looked at UICatalog under iOS 3, and Xcode 3 dot something. It's been building and running just fine for me. Then again, I do have to make minor changes to older projects to get them to build under Xcode 4.x, so it may be that I fixed that a long time ago.

UICatalog is the answer to all your questions, so it is very much worth your time to figure out how to build and run it.
 
It's fairly likely that the way you are trying to add your button to the screen (adding it directly to the window) is preventing you from seeing the button at all. As I said, you should not be dealing with your app's window at all. The template code from Xcode takes care of adding a root view controller and connecting it to the window, and you should rarely, if ever, change the contents of the window directly.

Just for Clarification: I have been examining this link on how to create interface buttons.

The author recommends declaring this code in the -(BOOL) ApplicationDidFinishLaunching method which is located in the App Delegate automatically generated by Xcode. No such method is in the view controller, hence me going with the author's instructions to write the code in the App Delegate.
 
Just for Clarification: I have been examining this link on how to create interface buttons.

That example is for Mac OS X, not iOS. It uses NSButton, which doesn't exist on iOS, and is a very different class than UIButton. That means it has the same problem you encountered before when converting from NSButton examples to UIButton examples.


With respect, I have. I downloaded it. When I tried to compile it and run it, it broke. Gave me 44 errors telling me I was trying to store various data in things not structures or unions.
Did you post that fact before? No.
Did you post the actual text of any error messages? No.
Have you told us what version of Xcode you're using, or that you require code that works with an older version? No.

No one here can help you solve problems if you don't tell us. We can't read your mind. We can't see your screen. We can't look on your disk and see what versions of software you're using.
 
No one here can help you solve problems if you don't tell us. We can't read your mind. We can't see your screen. We can't look on your disk and see what versions of software you're using.

you're quite right. I wasn't so focused on that issue, because I was trying to solve this other more pertinent one. I get 44 error messages when trying to compile UI catalog.

Errors range from:

(ControlsViewController.m)
Request for member 'color' in something not a structure or a union
At Top Level:
'Expected ')' before 'UIStepper'
'stepper' undeclared (first use in this function)
'UIStepper undeclared (first use in this function)
'Control reaches end of non-void function'

to

SegmentViewController.m
Undeclared selector 'setBackgroundImage: forState: barMetrics:
UIBarMetricsDefault' undeclared (first use in this function)
UISegmentedControl' may not respond to 'setBackgroundImage: forState: barMetrics:'
(messages without a matching method signature will be assumed to return 'id' and accept '…' as arguments)
UISegmenedControl may not respond to 'setDividerImage:forLeftSegmentState:rightSegmentState:barMetrics'
UITextAttributeTextColor undeclared (first use in this function)
 
So I am trying to adopt the 'ViewController' approach to hardcoding in UI buttons.



Code:
#import <UIKit/UIKit.h>

@interface uiTestViewController : UIViewController {
	UILabel *titleLabel;
	UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic, readonly, retain) UILabel *titleLabel;

-(void) displayButton;

@end

Code:
@implementation uiTestViewController

@synthesize titleLabel;
@synthesize window;

- (void) displayButton
{
	CGRect frame = CGRectMake(10, 40, 90, 40);
	
	
	//UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
	UIButton *button = [[UIButton alloc] initWithFrame: frame];
	
	
	button.titleLabel.font            = [UIFont systemFontOfSize: 12];
	button.titleLabel.lineBreakMode   = UILineBreakModeTailTruncation;
	button.titleLabel.shadowOffset    = CGSizeMake (1.0, 0.0);
	
	
	[self.window addSubview: button];
	
	
}

again, I get a successful compile, but no button appears.

I wanted to post the code in its entirety just so ppl would have a better idea of what I am doing (wrong).
 
Last edited:
So I am trying to adopt the 'ViewController' approach to hardcoding in UI buttons.



Code:
#import <UIKit/UIKit.h>

@interface uiTestViewController : UIViewController {
	UILabel *titleLabel;
	UIWindow *window;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property(nonatomic, readonly, retain) UILabel *titleLabel;

-(void) displayButton;

@end

Code:
@implementation uiTestViewController

@synthesize titleLabel;
@synthesize window;

- (void) displayButton
{
	CGRect frame = CGRectMake(10, 40, 90, 40);
	
	
	//UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
	UIButton *button = [[UIButton alloc] initWithFrame: frame];
	
	
	button.titleLabel.font            = [UIFont systemFontOfSize: 12];
	button.titleLabel.lineBreakMode   = UILineBreakModeTailTruncation;
	button.titleLabel.shadowOffset    = CGSizeMake (1.0, 0.0);
	
	
	[self.window addSubview: button];
	
	
}

again, still no result, but I wanted to post the code in its entirety just so ppl would have a better idea of what I am doing (wrong).

You need to show how the view controller is created and set up, as well as how/wen your your displayButton is called.

The "self.window addSubview" bit is wrong. You're not "adopting the view controller approach" at all when you do that.

For the third time, do not add things directly to the window object. The standard template code from Apple creates a view controller and adds that view controller's content view to the window. Everything else is added to the view controller's content view.
 
Here's the only way I think the ViewController is created:
Code:
#import <UIKit/UIKit.h>

@class uiTestViewController;
//@class UI

@interface uiTestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    uiTestViewController *viewController;
	
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet uiTestViewController *viewController;


@end


Code:
AppDelegate.m
#import "uiTestAppDelegate.h"
#import "uiTestViewController.h"

@implementation uiTestAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
	//self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
	// Set the view controller as the window's root view controller and display.
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

	//UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
	
		return YES;
}

edit: I just realized I am not actually calling '-displayButton' anywhere in my code. Oops. Should I call it in the -(BOOL) applicationDidFinishLaunching?
 
Last edited:
Here's the only way I think the ViewController is created...

And what about this part of the question (bolded)?:
You need to show how the view controller is created and set up, as well as how/wen your your displayButton is called.

If you're not going to provide complete answers, this is going to take much longer for you to put together your solution.
 
And what about this part of the question (bolded)?:


If you're not going to provide complete answers, this is going to take much longer for you to put together your solution.

err, yes well I realized my '-displayButton' method wasn't actually called. I turned it into a class method.

Code:
+(void) displayButton

how do I call this method to the ViewController.

The ViewController is already added to the AppDelegate via this code:
Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.

// Set the view controller as the window's root view controller and display.
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
	
		
return YES;
}
 
Last edited:
Here's the only way I think the ViewController is created:
Code:
#import <UIKit/UIKit.h>

@class uiTestViewController;
//@class UI

@interface uiTestAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    uiTestViewController *viewController;
	
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet uiTestViewController *viewController;


@end


Code:
AppDelegate.m
#import "uiTestAppDelegate.h"
#import "uiTestViewController.h"

@implementation uiTestAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
	//self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
	// Set the view controller as the window's root view controller and display.
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

	//UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
	
		return YES;
}

edit: I just realized I am not actually calling '-displayButton' anywhere in my code. Oops. Should I call it in the -(BOOL) applicationDidFinishLaunching?


This is like playing 20 questions, only worse. We're closing in on 50 posts in this thread, and you are still posting a couple of lines of your code here and there, but not the bits that matter.

POST YOUR ENTIRE application delegate, and all the code for your view controller, so the forum can get the whole picture.

Just because you define a view controller class does not mean that you are creating an instance of your view controller. You need an alloc/init somewhere, or in this case an alloc/initWithNibName:bundle:

TAKE A LOOK AT ONE OF THE EMPTY TEMPLATE PROJECTS THAT XCODE CREATES, LIKE THE SINGLE VIEW CONTROLLER PROJECT. It shows you how to create a view controller and install it in your window.

BTW, why are you using Xcode 3.x? It's old and out of date. Apple did a total rewrite of Xcode for Xcode 4.0, and we're now on Xcode 4.4, with Xcode 4.5 soon to follow. Learning Xcode 3.x is just going to teach you things that you will have to re-learn from the very beginning, since every single aspect of the user interface changed in Xcode 4. Learning Xcode 3.x is like learning how to do trig using a slide rule. You can <mostly> get the job done, but the skills you learn are no longer useful.

You should download the latest release version, Xcode 4.4.
 
BTW, why are you using Xcode 3.x? It's old and out of date. Apple did a total rewrite of Xcode for Xcode 4.0, and we're now on Xcode 4.4, with Xcode 4.5 soon to follow. Learning Xcode 3.x is just going to teach you things that you will have to re-learn from the very beginning, since every single aspect of the user interface changed in Xcode 4. Learning Xcode 3.x is like learning how to do trig using a slide rule. You can <mostly> get the job done, but the skills you learn are no longer useful.

You should download the latest release version, Xcode 4.4.

According to the OP's sig they are still running Mac OS X 10.6.8 (Snow Leopard) and Xcode 3.2.6 is the latest version that will run under that OS.

EDIT:

Oops, I was wrong! Xcode 4.2 is the latest version for Snow Leopard. OP should upgrade to that at least.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.