addSubview from different file - objective-c

In a game I am making, I made a class file called bomb. In the file, I have a method called displayBomb:
- (void) displayBomb
{
bombImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
}
The method is supposed to make an image display on the screen. In my ViewController.m file in ViewDidLoad, I have:
bomb *bomb1 = [[bomb alloc] init];
[bomb1 setValue:[UIImage imageNamed:#"image.png"] forKey:#"bombImage"];
[bomb1 displayBomb];
It doesn't display anything, though. I think the problem is that I need something like [self.view addSubview:bombImage]. When I put that in the ViewController.m file, it says Use of undeclared identifier 'bombImage'. When I put that in bomb.m, it says property "view" not found on object of type bomb *. I figure I need something like [ViewController.view addSubview:bombImage], but that returns property "view" not found on object of type ViewController.

You can't call a method on an object you don't have a reference to. Your view controller needs to know about bombImage or your bomb needs to know about your view controller. You could make bombImage a property of bomb and then in your view controller do this:
[self.view addSubview:bomb1.bombImage];
Edit: The typical way I'd assign an image on bombImage would be the following:
bombImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[bombImage setImage:[UIImage imageNamed:#"image.png"]];

add view parameter to your method:
- (void) displayBombOnView:(UIview *)view
{
bombImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[view addSubview:bombImage];
}
in viewDidLoad call method:
[bomb1 displayBombOnView:self.view];//or which view is on superview

Related

UIButton not being added to custom UIView Objective C

I am creating a custom UIView called DropDown that contains a button. In the .h file I initialize the button and in the constructor of the UIView, I add it to the frame and set the title. Here is the constructor:
-(id) initWithFrame:(CGRect)frame title1:(NSString*)text1{
self = [super initWithFrame:frame];
if (self){
_button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[_button1 setTitle:text1 forState:UIControlStateNormal];
_button1.frame = CGRectMake(10, 10, 50, 20);
[self addSubview:_button1];
}
return self;
}
In my ViewController viewDidLoad I am instantiating a DropDown object as follows:
_dropDown = [[DropDown alloc] initWithFrame:_dropDown.frame title1:#"Title"];
The _dropDown object is an IBOutlet and I hook it up on the view controller. When I run the application, no button is shown and I am unsure why. Any help would be much appreciated.
This statement
_dropDown = [[DropDown alloc] initWithFrame:_dropDown.frame title1:#"Title"];
references _dropDown.frame which is nil at this point because _dropDown hasn't been assigned yet. I would suggest creating the frame explicitly more like this:
_dropDown = [[DropDown alloc] initWithFrame:CGRectMake(10, 10, 50, 20) title1:#"Title"];
(you'll need to figure out the sizing for CGRectMake)

need Clarification on NSButton Creation and Usage

I have below code to create simple NSButton in a separate function
-(void)myFunction
{
NSButton *btn = [self createButton:#"Button_Name"];
if(some condition )
{
[btn setEditable:YES];
}
}
- (NSButton*)createButton:(NSString *)buttonName
{
NSButton *btn = [[NSButton alloc] initWithFrame:NSMakeRect(20, 0, 20, 20)];
[btn setButtonType:NSSwitchButton];
[btn setImagePosition:NSImageOnly];
[btn setTarget:self];
[btn setTitle: buttonName];
return btn;
}
In my same It is working fine.I am using this code in a Big project.Will It work normally or will cause some problem.Is this a correct way?
Few things I would like to bring in your notice:
You pass buttonName and buttonTitle but never uses it.
You create an object of type NSButton but your object name is against the convention, by reading btnCell someone will expect it to be NSButtonCell.
In the above code I cant see any reference to the newly created button and even you are not adding it to any view. (I hope in your real Big project you are not missing those.)

addSubview not showing up in NSView Objective C Cocoa

When I addSubview: nothing shows up. I set text and color to see it. Also if I manually add the view to the custom view in the UI bulder in xcode it shows up just fine with the text and color.
.m file
- (void)displayString:(NSString *)title {
NSRect frame = NSMakeRect(10, 10, 200, 17);
NSTextfield *newfield = [[NSTextField alloc] initWithFrame:frame];
[newfield setBezeled:NO];
[newfield setDrawsBackground:NO];
[newfield setEditable:NO];
[newfield setSelectable:NO];
[newfield setStringValue:title];
[newfield setTextColor:[NSColor blueColor]];
[test addSubview:newfield];
if([test.subviews containsObject:newfield]){
NSLog(#"view there"); // i get this message
}
if([newfield isHidden]){
NSLog(#"view hidden"); //i dont get this message
}
NSLog(#"view set");
}
test is a NSView (Custom view is what xcode calls it) that I have properly linked in.
So when I create the text field and add it to the NSView manually and then run that same code by adding text and color all works fine, this issue arrises when I try programmatically setting the view. Also I made sure it wasn't my creating of the view, as I have tried creating the view in the builder and not placing it in the NSView and then trying addSubview: but that also does not work. Let me know if you need more code.
DEVELOPMENT:
If the nsview (custom view) has an element already in it (manually added and can be anything) and I add the text field it works (I get both views in the nsview)? The subview is tested for and there, just cant see it.
You have to call initWithFrame: instead of just init
- (void)displayString:(NSString *)title {
NSRect frame = NSMakeRect(10, 10, 200, 200);
NSTextField *newfield = [[NSTextField alloc] initWithFrame:frame];
[newfield setStringValue:title];
[newfield setTextColor:[NSColor blueColor]];
[test addSubview:newfield];
NSLog(#"view set");
}
What type of view is test? Also you need to do a:
newfield.frame = CGRectMake(x,y,width,height)
in order to specify the look of the view
Turns out I set the view too early. I was under the impression that whatever you do to the view after its been set will be reflected on the view, but that seemed to be the issue. After altering the view to be exactly they way I want then set the view of the NSStatuditem.
so I get
[newfield setStringValue:title];
[newfield setTextColor:[NSColor blueColor]];
[test addSubview:newfield];
[statusItem setView:test];//this is the key, setting it after he changes.

Displaying map with Objective C

I am using this code for creating this map:
AGSTiledMapServiceLayer *tiledLayer = [AGSTiledMapServiceLayer
tiledMapServiceLayerWithURL:[NSURL URLWithString:#"http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"]]; [self.mapView addMapLayer:tiledLayer withName:#"Tiled Layer"];
[self addSubView:mapView];
return self;
I am returning the self and want to display it like this:
Mymap *myMap = [[Mymap alloc] initWithFrame:CGRectMake(20,20, 100, 100)];
[self.webView addSubView:myMap];
Nothing happens...any ideas?
Use storyboard or xib. Then, add uiview on which add mapview. Now, connect this mapOutlet to the specific class by control drag. And, that's it, it will show you the map on your device.

UIView size is not as expected to be

I can't figure out why that view takes the entire screen.
In AppDelegate file
...
self.viewController = [[[ViewController alloc]init]autorelease];
[self.window setRootViewController:self.viewController];
self.window.backgroundColor = [UIColor whiteColor];
..
In ViewController.m
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 30, 30)];
[view setBackgroundColor:[UIColor greenColor]];
self.view = view;
When I run the app the screen is entirely green instead of having just a square in green.
What is wrong here ?
The erroneous line is here:
self.view = view;
When you set a view of a UIViewController that is the root controller, it is guaranteed to fill the screen. Instead, add it as a subview:
[self.view addSubview:view];
And you should be fine.
The view controller automatically manages the size of its root view (self.view), so even if you initialize it with a smaller size it will later get resized to fill the screen. This resizing conveniently also happens when the interface orientation changes (see the answer this question).
As suggested by Richard's answer, you can add your green view as a subview to the controller's root view. The crash you get is probably because the root view does not exist yet when you try to access it. Try the following:
- (void) loadView
{
[super loadView]; // creates the root view
UIView* subView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 30, 30)];
[subView setBackgroundColor:[UIColor greenColor]];
// because you don't set any autoresizingMask, subView will stay the same size
[self.view addSubview:subView];
}