Can't get subview animation to appear even after alloc :) - xcode

I have a subview loaded into an UIView. In the subview's .m file I have the following:
- (void)startAnimation {
// Array to hold png images
imageArray = [[NSMutableArray alloc] initWithCapacity:22];
animatedImages = [[UIImageView alloc] initWithImage:viewForImage];
// Build array of images, cycling through image names
for (int i = 1; i < 22; i++){
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"image%d.png", i]]];
}
animatedImages.animationImages = [NSArray arrayWithArray:imageArray];
// One cycle through all the images takes 1 seconds
animatedImages.animationDuration = 2.0;
// Repeat forever
animatedImages.animationRepeatCount = 0;
// Add subview and make window visible
[viewForMovie addSubview:animatedImages];
// Start it up
animatedImages.startAnimating;
NSLog(#"Executed");
}
Please be noted that I have in the .h file:
UIImageView *animatedImages;
NSMutableArray *imageArray;
UIView *viewForMovie;
#property(nonatomic,retain)IBOutlet UIView *viewForMovie;
and in the .m file:
#synthesize viewForMovie;
and I have connected viewForMovie to a UIView in IB. I've been on this for several hours now and have tried many variations I've found on the web but cannot get it to work. There are no errors and the other GUI graphics in the subview appear very nicely....but the animation just doesn't appear over top where it should. Also the NSlog reports that the method has in fact been called from the parent. Can anyone see any blaring issues? Thx.
PS: I'm pretty new at this.

Based on the code shown and the behavior you see so far, here are my suggestions:
Make sure the viewForMovie IBOutlet is connected properly in Interface Builder. If it's not connected properly (and so nil), nothing will appear. If you didn't mean to make it an IBOutlet in the first place, then you'll need to manually create it and add it as a subview to self before using it.
Not sure why you have the viewForMovie UIView in the first place. Is this subview's class (let's call it MySubview) a subclass of UIView? You can just show the animation in self instead of adding another subview inside it. Are you going to add more uiviews to this subview besides the viewForMovie?
To get rid of the "may not respond to" warning, declare the startAnimation method in the MySubview.h file (under the #property line):
-(void)startAnimation;
The fact that the warning says "UIView may not respond" also tells you that the parent view has declared newView as a UIView instead of MySubview (or whatever you've named the subview class). Change the declaration in the parent from UIView *newView; to MySubview *newView;.
In the initWithImage, what is "viewForImage"? Is it a UIImage variable or something else?
If all of the images are the same size and fit in the subview as-is, you don't need to set the frame--the initWithImage will automatically size the UIImageView using the init-image dimensions.
Double check that the images you are referencing in the for-loop are named exactly as they are in the code and that they have actually been added to the project.
Finally, you should release the objects you alloc in startAnimation. At the end of the method, add:
[imageArray release];
[animatedImages release];
The only item, however, that I think is actually preventing the animation from appearing right now is item 1.

Related

How to set buttons position?

I'm working on an iPad application. On the main screen there are 6 buttons which was created in IB. I'd like to arrange them dynamically from code, but I can't.
Already tried setting the frame/center of the objects, but they didn't move.
I have an array which holds the button outlets, and arrange them in a for loop.
Is there any suggestion?
thanks!
edit:
Here's some code(not the whole, but the rest is irrelevant now), how I loop through the buttons. Button outlets already set properly. The title of the buttons changes properly, also I can hide/show the buttons, but not the position.
If I'll just try some dummy code like:
tempBtn.frame = CGRectMake(100.0, 200.0, 60.0, 40.0);
nothing changes(but I suspect all the 6 buttons should be at the same position)
// I have this array, which contains the button outlets
tAddBtnArray = [NSArray ArrayWithObjects:tAddBtn1,tAddBtn2,tAddBtn3,tAddBtn4,tAddBtn5,tAddBtn6, nil];
//I loop through the array, set button title, and trying to change the y position
for(int i=0;i<6;i++){
UIButton *tempBtn = [tAddBtnArray objectAtIndex:i];
[tempField setText:#"Set some title"];
}
You can add uibbutons programatically in this way:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(yourMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Jesus Gil y Gil" forState:UIControlStateNormal];
button.frame = CGRectMake(100.0, 200.0, 60.0, 40.0);
[view addSubview:button];
I bet I know what it is. Do you have autolayout enabled on the xib file? If so, the frame is being set, and then the constraints are being evaluated, which move the buttons back to where they were assigned.
You have two options:
Turn off autolayout, so that you can manage the frames yourself.
Leave autolayout on, but update the constraints as well as the frame.
I would suggest turning autolayout off, but I'm just not particularly fond of it, so it is just a preference.

Place a background Image behind a grouped UITableView

I've a grouped UITableView which has been resized and rounded. I would like to place a view BEHIND this table.I've tried:
[self.tableView addSubview:backgroundView];
[self.tableView sendSubviewToBack:backgroundView];
but it didn't work. This is what I obtain:
I would like to have the background in place of the black space.
Any idea?
How about [self.tableView setBackgroundView:backgroundView];?
Bear with me I'm a beginner. I just ran into this problem and I found two solutions. In my case, I was using a UITableView object.
To begin, add the background image into your app's directory. I put mine in "Supporting Files"
You can add the image as a UIColor object:
...
[resultsTable setBackGroundColor:setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.jpg"]]];
...
The problem with this method is that it will repeat your background image because it's treating it as a pattern.
The better way:
// Create a string to store the path location of your image, otherwise you would have to provide an exact path name
NSString *backPath = [[NSBundle mainBundle] pathForResource:#"background"
ofType:#"jpg"];
// Create a UIImage Object to store the image.
UIImage *bgImage = [[UIImage alloc ] initWithContentsOfFile:backPath];
// Create a UIImageView which the TableView
UIImageView *bgView = [[UIImageView alloc]initWithImage:bgImage];
// Set your TableView's background property, it takes a UIView Obj.
[resultsTable setBackgroundView: bgView];
.....

Unable to get tab order working within NSPopover

I have a View within an NSPopover, and I am unable to set the tab order correctly. I have set the nextKeyView within my 4 text fields. But it tends to flip from TextField1 to Search1, instead of TextField1 -> TextField2. I have tried inserting [self.view.window makeFirstResponder:textField1] also [self.view.window setInitialFirstResponder:textField1] along with recalculatekeyviewloop but with no luck.
Any help would be much appreciated.
I had a similiar problem when composing the popover-view of certain subviews programatically in awakeFromNIB. I could solve the problem by inserting the subviews after the popover had its private NSPopoverWindow set (i.e. it was shown the first time). It seems like the popover is re-evaluating the view-loop when the popover-view is embedded in the private child-window - ignoring the view-loop given in the view.
You could try the following:
-(void) popoverDidShow:(NSNotification *)notification{ // NSPopoverDelegate-method
if (!popoverDidShowForTheFirstTime){
[self setUpViews];
}...
-(void) setUpViews{
popoverDidShowForTheFirstTime = YES;
// insert views and establish nextKeyViews ...

Cocoa Objective-C adding subviews

I have a game application (cocoa, not cocoa touch) where I am trying to add map elements. My app window is named mainWin. I have a subview of mainWin named viewGameMap, which I added in IB. I have a class called room.h/room.m which basically takes dimensions generated in the app for width and height of the room.
I can do this:
room* thisRoom = [[room alloc] initWithFrame: NSMakeRect(441.0-roomWidth,520.0-roomLength, roomWidth * 10, roomLength * 10)];
[[mainWin contentView] addSubview:thisRoom];
But I really want to add this subview to viewGameMap, can I subview a subview? In IB I noticed that a subview doesn't have a contentView so I'm not sure how I would place it there.
In a related question, these room views wouldn't be permanent so how do I destroy them once I am finished with them.
Thanks
First, if you declare viewGameMap in your .h file, like this (using #property directive):
#property (nonatomic, retain) IBOutlet UIView *viewGameMap;
or even just in actual declaration, like this:
IBOutlet UIView *viewGameMap;
then you should be able to bind viewGameMap to appropriate UIView within IB. This will give you direct access to viewGameMap.
Second, since you are using alloc to instantiate your Room object (n.b., class Room should be capitalized per convention), you are responsible for it (you own it). But when you call -addSubview:, then [mainWin contentView] also owns thisRoom. So, you can do this:
room* thisRoom = [[room alloc] initWithFrame: NSMakeRect(441.0-roomWidth,520.0-roomLength, roomWidth * 10, roomLength * 10)];
[viewGameMap addSubview:thisRoom];
[thisRoom release];
Later, when thisRoom is removed from [mainWin contentView], its reference count will (barring some other referencing) drop to zero and it will, ultimately, get deallocated.

How do I get the inner/client size of a NSView subclass?

I am doing manual layouting for my Cocoa application and at some point I need to figure out what the inner size of a NSView subclass is. (E.g. What is the height available for my child view inside of a NSBox?)
One of the reasons is that I am using a coordinate system with origin at the top-left and need to perform coordinate transformations.
I could not figure out a way to get this size so far and would be glad if somebody can give me a hint.
Another very interesting property I would like to know is the minimum size of a view.
-bounds is the one you're looking for in most views. NSBox is a bit of a special case, however, since you want to look at the bounds of the box's content view, not the bounds of the box view itself (the box view includes the title, edges, etc.). Also, the bounds rect is always the real size of the box, while the frame rect can be modified relative to the bounds to apply transformations to the view's contents (such as squashing a 200x200 image into a 200x100 frame).
So, for most views you just use [parentView bounds], and for NSBox you'll use [[theBox contentView] bounds], and you'll use [[theBox contentView] addSubview: myView] rather than [parentView addSubview: myView] to add your content.
Unfortunately, there is no standard way to do this for all NSView subclasses. In your specific example, the position and size of a child view within an NSBox can be computed as follows:
NSRect availableRect = [someNSBox bounds];
NSSize boxMargins = [someBox contentViewMargins];
availableRect = NSInsetRect(availableRect, boxMargins.width, boxMargins.height);
If you find yourself using this often, you could create a category on NSBox as follows:
// MyNSBoxCategories.h
#interface NSBox (MyCategories)
- (NSRect)contentFrame;
#end
// MyNSBoxCategories.m
#implementation NSBox (MyCategories)
- (NSRect)contentFrame
{
NSRect frameRect = [self bounds];
NSSize margins = [self contentViewMargins];
return NSInsetRect(frameRect, margins.width, margins.height);
}
#end
And you would use it like so:
#import "MyNSBoxCategories.h"
//...
NSRect frameRect = [someNSBox contentFrame];
[myContentView setFrame:frameRect];
[someNSBox addSubview:myContentView];
The bounds property of NSView returns an NSRect with the origin (usually (0,0)) and the size of an NSView. See this Apple Developer documentation page.
I'm not sure (I never had to go too deep in that stuff), but isn't it [NSView bounds]?
http://www.cocoadev.com/index.pl?DifferenceBetweenFrameAndBounds

Resources