That brought up no errors, but it also didn't do anything. It isn't putting anything in UIView mainView. Everything is returned nil, and nothing is displayed in the scroll view in the app.
map1MainView, map2MainView, ... are variables I declared in the header file and synthesized in the implementation file that are connected to isolated views in Interface Builder. I'm trying to turn the string into a variable.
That brought up no errors, but it also didn't do anything. It isn't putting anything in UIView mainView. Everything is returned nil, and nothing is displayed in the scroll view in the app.
map1MainView, map2MainView, ... are variables I declared in the header file and synthesized in the implementation file that are connected to isolated views in Interface Builder. I'm trying to turn the string into a variable.
You're trying to look up an object by using a string which is the name of that object?
There's not really a way to do that.
The closest you can come is to put all the objects you want to look up by name into a dictionary:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Use an NSMutableArray to hold your five mapviews. Looking up a variable by name is the wrong problem to solve - you're really trying to loop through a list of views, and an array is the right way to do that.
Thanks guys that's exactly what I was looking for! One quick question, can you tell me if I'm doing this right? I'm trying to add an image to one of the Main Views:
Code:
UIImage *image1 = [UIImage imageWithContentsOfFile:@"Graphic Elements/Map 1 Main Elements/1 Main Floating Cube.png"];
UIImageView *map1MainView1 = [[UIImageView alloc] initWithImage:image1];
[map1MainView addSubview:map1MainView1];
Does a lot of your code look similar to this? You sure are leaking a lot of stuff there... mainViewName, mainView and log. Plus you're really going out of your way to leak your log string. NSLog was intended to be a one-step operation; it takes a format string plus as many arguments as you need.
__________________ Recall It!Tag your notes. Tag your photos. Tag your thoughts. Tag your life.
Does a lot of your code look similar to this? You sure are leaking a lot of stuff there... mainViewName, mainView and log. Plus you're really going out of your way to leak your log string. NSLog was intended to be a one-step operation; it takes a format string plus as many arguments as you need.
I'm pretty new, honestly I didn't *really* know what leaking is until you posted this. So I'm assuming leaking is when you allocate something and don't release it?
Anyways I'm a little deep into my project, I'm pretty sure I can add all the releases later.
P.S. I found what was wrong with the Image loading thing, I had to use imageNamed instead of initWithContentsOfFile.
I'm pretty new, honestly I didn't *really* know what leaking is until you posted this. So I'm assuming leaking is when you allocate something and don't release it?
Anyways I'm a little deep into my project, I'm pretty sure I can add all the releases later.
P.S. I found what was wrong with the Image loading thing, I had to use imageNamed instead of initWithContentsOfFile.
Memory management is perhaps the single most important thing to get right about Cocoa development. If you don't learn the rules of memory management and apply them religiously, your apps will be buggy and subject to unexpected crashes. You will get very frustrated and decide that iOS programming is impossible.
I urge you to stop now, read the memory management programming guide, and don't write another line of code until you understand the rules and can follow them consistently. Writing a bunch of code and waiting to "add the releases later" is a big, big mistake. It's like building a concrete bridge and assuming you can add the steel reinforcement later. You'll be too busy dealing with collapsing structures to have time to fix everything. Really.
The good news is that the rules of Cocoa memory management are actually quite simple. I suggest you read this:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Thanks for the advice. I didn't understand it the first time I read it (when I had JUST wanted to start programming) but I'll read it again now. Hopefully it'll make more sense to me now.
NSLog([[NSString alloc] initWithFormat:@"Inserted %@ in cvMainViewScrollView at index %d!", mainViewName, mainViewNumber]);
How would I release that?
UPDATE: I have a few questions you can hopefully answer:
1) The last question
2) Is "release" the only thing I need to know? Should I look into what dealloc is? What's the difference?
3) If I tell the app delegate to switch rootviewcontroller, does it release everything from the last rootviewcontroller? if not, under what method should I put all my releases?
4) If I switch views using the [UIView transitionFromView:toView:duration: ...] does it release everything from the last view?
5) How do I remove/release a specific subview?
UPDATE 2: Yeah I tried turning all those strings into something like this:
Code:
NSLog([NSString initWithFormat:@"Inserted %@ in cvMainViewScrollView at index %d!", mainViewName, mainViewNumber]);
and nothing works now, it seems I have to allocate it. I just don't know how to release it cause it has no name.
UPDATE 3: Changed all those initWithFormats to stringWithFormats. Problem solved. Object Ownership and Disposal = very useful information. Thanks.
Last edited by UnretroGamer; 05-30-2011 at 06:24 PM.
Reason: UPDATE 3
NSLog([[NSString alloc] initWithFormat:@"Inserted %@ in cvMainViewScrollView at index %d!", mainViewName, mainViewNumber]);
How would I release that?
The way you've written it, you can't. You're creating an object with alloc/init and not saving a pointer to it, so there's no way to release it.
NSLog takes a format string and parameters. You should rewrite it like this:
Code:
NSLog(@"Inserted %@ in cvMainViewScrollView at index %d!"", mainViewName, mainViewNumber);
Actually, you could set up your string to be released later, by using autorelease. The method autorelease tells the system to send your object a release message after this pass through the event loop. The object will stick around until your code returns, and then get its release message once your code is done. You'd just enclose your alloc/init call in another set of brackets, with "autorelease" before the last closing bracket. However, don't do that. As I said, NSLog is written to take a format string and a variable number of parameters. Use that feature instead of creating a new string.
Quote:
UPDATE: I have a few questions you can hopefully answer:
1) The last question
2) Is "release" the only thing I need to know? Should I look into what dealloc is? What's the difference?
All this is covered in the memory management guide, and explained better than I can explain it.
retain and release are opposites. They cause an object's retain count to be increased by one / decreased by 1.
An object can have more than one owner. The retain count is used to keep track of how many owners an object has. When an object's retain count drops to zero, it gets deallocated (That means its memory is freed and it goes away.)
autorelease, as I mentioned above, is a variant of release that adds your object to a list of objects that will get a release message once your code returns. It's useful for creating temporary objects and forgetting about them. Many of the system methods like the NSString stringWithFormat method return autoreleased objects. They stick around until your code returns, and then get discarded unless somebody else sends them a retain message.
An object's dealloc method gets called right before the object is freed (taken out back and shot.) It's the object's chance for a final cigarette - or rather, an object's chance to do cleanup, and send release messages to any objects that it owns.
3) If I tell the app delegate to switch rootviewcontroller, does it release everything from the last rootviewcontroller? if not, under what method should I put all my releases?
You can't change root view controllers. The root view controller of a navigation controller is fixed, at least as far as I know.
Quote:
4) If I switch views using the [UIView transitionFromView: toView:duration: ...] does it release everything from the last view?
I haven't used that method before. My company's apps to date have been written to work on iOS 3.2 or later, and that method requires 4.0. I'm honestly not sure if it removes the old view from it's superview. This bit from the documentation makes it sound like the old view is removed from it's superview (and thus released) at the end of the animation:
Code:
"By default, the view in fromView is replaced in the view hierarchy by the view in toView"
Usually, views are retained by their parent view (or "superview") The outermost view is retained by the view controller. When the view controller is released, it releases it's content view. The content view releases it's sub-views, which in turn release their sub-views
Quote:
5) How do I remove/release a specific subview?
You can send a view a -removeFromSuperview message. That causes it to be removed from it's parent view. The parent view sends it a release, which causes it to be deallocated unless somebody else has also retained it.
Get yourself a book on Cocoa programming and read it. I'm about tapped out answering questions that are really well documented by Apple and well described in dozens of books.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
I'm sorry your tired of it. I bought two $50 books that haven't helped nearly as much as this forum (half of the questions answered specifically by you). I hope my questions will get more advanced and interesting for you in the future. Anyways, you've been a huge help! Thanks for all the advice and solutions.
P.S. You can switch root view controllers. Tell the App Delegate to do this: