I have experience on windows client dev, language c++, and Flash ActionScript3 dev, but i’m quite a beginner on iOS dev…
to make it really detailed this post may be a little too long…
my Xcode ver is 4.2.1
i created an iOS Application(Empty Application), and there are only (MyAppDelegate.h) and (MyAppDelegate.m), if i compile and run it, there will be a white board displayed, and nothing more, that we all know.
now, i want to draw a picture, or a simple rect or whatever something on the screen, and i don’t want to use storyboard, so i create a new Objective-C class: MyView, which inherit from UIView. and override the function – (void)drawRect:(CGRect)rect
then i write something like this in MyAppDelegate.m’s didFinishLaunchingWithOptions function:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController.view = [[MyView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window.rootViewController.view drawRect:[[UIScreen mainScreen] bounds]];
return YES;
}
so you see, what i do is alloc a new MyView instance and assign it to self.window.rootViewController.view, and then invoke drawRect function.
but when i put a breakpoint in MyView’s drawRect func and debug, it didn’t stop there, that means the app never went there, i really can’t figure out why.
i read Docs about View and ViewController but they did’t tell this problem , hope you can help me! and please tell me what should i read to know more about this topic
First off NEVER call drawRect directly, the system calls this. Secondly in your example you have no view controller so in fact rootViewController is nil, hence why nothing is working.
You should either create a view controller to manage your view OR simply add your UIView subclass to the window directly…
NOTE: You should be using a UIViewController to manage this for you however!
If you are new to objective-c then I would start with a UIView project rather than an empty one, and take a look how everything hangs together.