I am building my own view in loadView of view controller. Just to check if I can release the view like below or there is anything else I will need to release it? I know framework will set it to nil once it requires to free up some memory.
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[self.view release];
...
}
That looks a bit weird, I'd do this instead:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
self.view = view;
[view release];
If you're using #property (retain) you don't have to worry about releasing it when it's set to nil e.g. self.view = nil;. However you should release it in dealloc.
The view property of a UIViewController will be released for you by the UIViewController itself. You are right however that when you create the view that you assign to a UIViewController you need to release it, but the way you are trying to do it is wrong. Either do what jtbandes suggest or simply:
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)] autorelease];
Related
I need to add a Button or a label to a UIView and add it to my UIViewController. I did the following but it only crashed the program.
UIView *myview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 0, 300)] ;
[myview addSubview:myButton];
[self.view addSubview:myView];
The code crashes. Why is that?
You set your myview's width to 0.
You sure you created "myButton"?
You create myview but you add my*V*iew as subview. Is there any warning?
The first step you need to take is:
Make Sure you have allocated & initialized UIButton object with "initWithFrame" method.
provide a width to your UIView in "initWithframe" method, So that it can be seen.
Now use your [self.view addSubview:myView];
Please let me know if it is useful.
Use:
//Assuming that myview should have the size of the main view:
UIView *myview = [[UIView alloc] initWithFrame:self.view.bounds];
[myview addSubview:myButton]; //Whatever ...
[self.view addSubview:myview]; //There was a typo in this line
If this code block is in the init or loadView of viewcontroller, this will crash.
Make sure you have created a view for view controller in this case before adding views to self.view
UIView *aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = aView;
[aView release];
myButton = [[UIButton alloc] init....
.....
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 0, 300)] ;
[myView addSubview:myButton];
[self.view addSubview:myView];
Try updating the view with the following:
[myview setNeedsDisplay];
and remove the second call to addSubView (which is probably causing the crash):
[self.view addSubview:myView];
I want to make window based app without using IB.
I created MyViewController by allocating it.
Do I have to create view inside MyViewController also?
Without allocating it, app crashes.
For UITableViewController, UINavigationController,... I don't have to allocate view.
What is difference?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[MyViewController alloc]init];
viewController.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.window addSubview:viewController.view]; // app crashes here without allocating view explicitly
}
you should either allocate the view explicitly just as you shown in your code or implement "loadView" in your controller so that the view is created when it is first shown on screen.
MyViewController *_myViewController = [[MyViewController alloc]init];
UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
_myViewController.view = myView
[myView release];
[self.window addSubview:myViewController.view];
I was confuse with memory management of removeFromSuperview.
Here is my code:
MySubView *tMySubView = [[MySubView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
tMySubView.center = self.view.center;
tMySubView.tag = 1111;
[self.view addSubview:tMySubView];
[tMySubView release];
"self" is UIViewController.
When "self" call dealloc but MySubView didn't call dealloc.
I know addSubView retainCount +1.
So I try add [tMySubView removeFromSuperview] in "self" dealloc
And MySubView dealloc was called...
Should I add [subView removeFromSuperview]; when superView dealloc?
Or superView removeFromSuperview ,it will automaticly call subView's removeFromSuperview...?
I can't figure it out. :(
Thanks!
It means that self.view is not properly released. It will remove all it's subviews only in case when it is deallocated.
Check your loadView method (where you're probably initialize it). If it looks like this:
-(void) loadView {
self.view = [[UIView alloc] init];
}
then you've got leak and have to rewrite it like this:
-(void) loadView {
self.view = [[[UIView alloc] init] autorelease];
}
OR
-(void) loadView {
UIView* v = [[UIView alloc] init];
self.view = v;
[v release];
}
Also check all the places where you're accessing self.view and make sure you're not over-retaining it.
This is what I tried. Nothing appears on the screen and none of the UITableView methods that you are supposed to implement are getting called.
-(void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = view;
[view release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UITableViewController *TVC = [[[UITableViewController alloc]
initWithStyle:UITableViewStylePlain] autorelease];
CGRect cgRct = CGRectMake(0, 10, 320, 100);
UIView *newView = [[UIView alloc] initWithFrame:cgRct];
TVC.view = newView;
[newView release];
[self.view addSubview:TVC.view];
}
I've looked for good examples and tutorials on doing this programmatically but there are none.
What I am trying to achieve is a Table that doenst take up my who screen. Maybe 3/4 of my screen would be good.
Many Thanks
Code
The problem is that you're creating a UITableViewController, which is a UIViewController, and will expect to be on the nav stack. What you want to do is create a UITableView, which is a UIView. You are also not setting the table's delegate and data source, which you will need to do to get calbacks.
Your viewDidLoad should look something like this
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *table = [[[UITableView alloc] initWithStyle:UITableViewStylePlain] autorelease];
table.dataSource = self;
table.delegate = self;
table.frame = CGRectMake(0, 10, 320, 100);
[self.view addSubview:table];
}
(Note that if you're going to need access to the table outside of the callbacks, you should save it in an ivar rather than declaring it locally, and should retain it. Let me know if you need a few more lines of code to show you what I mean)
Make sure you set the delegate of TVC, with
TVC.delegate = self;
That's the reason why none of those methods are getting called. Also, make sure your class implements the UITableViewDelegate protocol by changing your interface declaration to
#interface YourViewController : UIViewController <UITableViewDelegate> {
//declare variables here
}
Also, equally important, don't set TVC.view, as this already happens when you initialize the view controller. You're just setting it to a blank view, which is why you're not seeing anything.
iOS7 seems to like this way of init'ing the tableview:
//make tableview
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 81, 200, 200) style:UITableViewStylePlain];
table.dataSource = self;
table.delegate = self;
[self.dataView addSubview:table];
try that out. Hope it helps someone.
UIView *newView = [[UIView alloc] initWithFrame:cgRct];
TVC.view = newView;
I'll give you a hint. Here you are setting the view of the UITableViewController to an EMPTY VIEW...
I'm getting this gap at the bottom of my window after I add the view of a custom UIViewController. The gap goes away as the view is shifted down after I move to another view and then back.
Here is the code in the app delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
CustomViewController *gvc = [[CustomViewController alloc] init];
self.customViewController = gvc;
[gvc release];
[window addSubview:customViewController.view];
[window makeKeyAndVisible];
}
"CustomViewController" is used as a root view controller to simply coordinate which other UIViewControllers to display. As such I simply set its view = to that of the first ViewController's view needed like so:
- (void)loadView {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = v;
[v release];
// Add the HomeViewController to the top of the stack
MainViewController *mvc = [[MainViewController alloc] initWithNibName:#"MainView" bundle:nil];
mvc.delegate = self;
self.mainViewController = mvc;
[mvc release];
[self.view addSubview:self.mainViewController.view];
}
I've tried a bunch of things including what you see here with no luck. Any ideas?
thanks
This line
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
should be ...
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
Good times.