Displaying map with Objective C - 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.

Related

Error with MKMapView

When I create the story board below the map over takes the entire screen. This is understandble with the code that I have below it uses the entire bounds of the view. However, if I do a normal "init", I lose functionality of the map! The map usually zooms into my location, and a few other things. When I use init instead of initWithFrame the map doesn't go to my location.
self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds];
self.myMapView.delegate=self;
How do I initialize the map without having it take over the entire screen like shown in my story board. New to iOS, thanks for your help!
//Setting up the location manger for finding the User's location
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
//Check to see if they have Authorization to use to use location servieces
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
//Initialize the map and specifiy bounds
self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds];
//Set the delegate to itself
self.myMapView.delegate=self;
//Set a standard mapview, Enable scrolling and zooming
self.myMapView.mapType = MKMapTypeStandard;
self.myMapView.scrollEnabled = YES;
self.myMapView.zoomEnabled = YES;
//specifcy resizing
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
//show the User's location and set tracking mode
self.myMapView.showsUserLocation = YES;
self.myMapView.userTrackingMode = MKUserTrackingModeFollow;
//add the VIEW!!
[self.view addSubview:self.myMapView];
//Set up the UISearch Bar
self.mySearch = [[UISearchBar alloc]init];
self.mySearch.delegate = self;
//Set the initial Text
self.mySearch.prompt = #"Enter Address or Use Current Location";
There's two approaches to create your map view.
1.Create in code. As shown in your example. You need to set the map view frame. If you need firstly to create UISearchBar, you should set map view frame according to search bar position. It might look like this.
self.mySearch = [[UISearchBar alloc] init];
self.mySearch.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
//...
self.myMapView = [[MKMapView alloc] init];
self.myMapView.frame = CGRectMake(0, self.mySearch.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.mySearch.frame.size.height);
//...
2.Much more simpler way - create in storyboard. Firstly, there no need to create your outlets (such as search bar and map view) in code. You simple drag'n'drop all the controls to view. Open your storyboard, set the view controller class from UIViewController to your class name.
Note that the properties must be IBOutlets
#property (nonatomic,weak) IBOutlet MKMapView *myMapView;
#property (nonatomic,weak) IBOutlet UISearchBar *mySearchBar;
After dropping your controls to view, you must connect visual controls to your class outlets. Take a look at this tutorial where connections are explained.

addSubview from different file

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

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.

How to increase the height of NSTableHeaderView?

I need to implement a headerview with specific size and gradient. I have to insert images in certain cells of the headerview.Tried to create the cells for the headerview using the following code,but i was not able to customize the headerview.
[[tableColumn headerCell] setImage:[NSImage imageNamed:#"sampleHeader"]];
If I use the overridden subclass of headerview, I was not able to view the images or text in the header cell.Please provide me any pointers to solve this issue.
I was able to insert images and text by subclassing the NSTableHeaderCell.How to increase height of the NSTableHeaderView?
If I subclass both NSTableHeaderView and NSTableHeaderCell , was not able to view anything in the
headercell.I used the following code for setting headerview and headercell
[tableView setHeaderView:CustomHeaderView];
[tableColumn setHeaderCell:[[[CustomHeaderTableCell alloc] initImageCell:
[NSImage imageNamed:#"sample"]]autorelease]];
I have the same issue as given in the below url
http://lists.apple.com/archives/cocoa-dev/2002/Jun/msg00331.html
You don't need to subclass NSTableHeaderView.
I was able to change the height of the header view using the following snippet in the controller class:
-(void)awakeFromNib {
NSRect frame = tableView.headerView.frame;
frame.size.height = 26;
tableView.headerView.frame = frame;
}
It should be noted that the scroll view takes care of the layout. It automatically changes the frame of the headerView as necessary, but leaves the height intact. Resizing the clip view etc as suggested in the other answer is not necessary.
You can also create a NSTableHeaderView object, initialize it with a frame(rect with height and width) and set that NSTableHeaderView object to your table view.
NSTableHeaderView *tableHeaderView = [[NSTableHeaderView alloc] initWithFrame:NSMakeRect(0, 0, 120, 60)];
[myTableView setHeaderView:tableHeaderView];
[tableHeaderView release];
Following link helped me in solving the issue.
http://lists.apple.com/archives/cocoa-dev/2003/Feb/msg00676.html
You need to set the Frame for NSClipView, NSTableHeaderView and the CornerView
This is how I implemented the same in Code.
for(NSView * subview in [topScrollView subviews])
{
for(NSView * subSubView in [subview subviews])
{
if([[subSubView className] isEqualToString:#"NSTableHeaderView"] && [[subview className] isEqualToString:#"NSClipView"])
{
[subSubView setFrameSize:NSMakeSize(subSubView.frame.size.width, subSubView.frame.size.height+5)];//HeaderView Frame
[subview setFrameSize:NSMakeSize(subview.frame.size.width, subview.frame.size.height+5)];//ClipView Frame
}
}
if ([[subview className] isEqualToString:#"_NSCornerView"])
{
[subview setFrameSize:NSMakeSize(subview.frame.size.width, subview.frame.size.height+5)]; //CornerView Frame
}
}

Programmatically creating an NSTableView (trouble getting the NSHeaderView to show up)(cocoa osx)

I am making an NSTableView programmatically but for some reason no matter what I do, I cannot make the darn headerView show up. It is imperative that I do this programmatically and not use the IB because I am actually developing this widget in an IDE called clozure cl which is a lisp ide that includes a cocoa bridge. Originally I thought this problem might have been caused by my development environement but I just created an example in Xcode using only objective C and it seems that the problem persists. What I do is pretty straightforward:
I make a window in the IB and in its awkefromnib methods I create and setup a table-view here is the code:
- (void)awakeFromNib {
mydatasource *data = [[mydatasource alloc] init];
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:#"id"];
NSTableView *table = [[NSTableView alloc] initWithFrame: [[self
contentView]frame]];
[table setDataSource:data];
[table addTableColumn:column];
[table addTableColumn:column];
[[self contentView] addSubview:table];
}
Here is the code for my data source object:
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
printf("NUM ROwS");
return 4;
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
printf("THE OTHER ONE");
return #"OKAY";
}
With this code I get a window with two colums and four rows and each cell displaying the string "OKAY", this is all fine and good except the table has no header. This might make sense except when I look at the tables header method, it has an initialized header with a frame whose values make sense. I am just wondering why I do not see it. Is there some special kind of magic I need to do so the header will display? I cannot seem to find any clues in the documentation. Once again it is imperative for the lisp ide that this be done programmatically so it would not be helpful no suggest using the IB which I know will have a working headerView. Thanks a lot.
Here is some code to programmatically create a table view with a scroll bar, and multiple columns.
// create a table view and a scroll view
NSScrollView * tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(10, 10, 380, 200)];
NSTableView * tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, 364, 200)];
// create columns for our table
NSTableColumn * column1 = [[NSTableColumn alloc] initWithIdentifier:#"Col1"];
NSTableColumn * column2 = [[NSTableColumn alloc] initWithIdentifier:#"Col2"];
[column1 setWidth:252];
[column2 setWidth:198];
// generally you want to add at least one column to the table view.
[tableView addTableColumn:column1];
[tableView addTableColumn:column2];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView reloadData];
// embed the table view in the scroll view, and add the scroll view
// to our window.
[tableContainer setDocumentView:tableView];
[tableContainer setHasVerticalScroller:YES];
[[self contentView] addSubview:tableContainer];
[tableContainer release];
[tableView release];
[column1 release];
[column2 release];
Just thought I would post this for anyone still looking for a straight forward answer. :)
I wrote this Swift version of #Alex Nichol's answer:
let tableContainer = NSScrollView(frame:NSMakeRect(10, 10, 380, 200))
let tableView = NSTableView(frame:NSMakeRect(0, 0, 364, 200))
let column1 = NSTableColumn(identifier: "Col1")
let column2 = NSTableColumn(identifier: "Col2")
column1.width = 252
column2.width = 198
tableView.addTableColumn(column1)
tableView.addTableColumn(column2)
tableView.setDelegate(self)
tableView.setDataSource(self)
tableView.reloadData()
tableContainer.documentView = tableView
tableContainer.hasVerticalScroller = true
window.contentView.addSubview(tableContainer)
It worked.
Well I answered my own question and I thought this might be helpful to someone else, it seems that that the headerView will only show up if you add the tableview to a scrollview.