I'm trying to add a map to my ViewController but it crashes on the init method. This is my code:
In TAMap.h:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface TAMap : UIViewController<MKMapViewDelegate>
#property (nonatomic, strong) MKMapView *mapView;
#end
In TAMap.m:
#implementation TAMap
#synthesize mapView = _mapView;
- (void)viewDidLoad {
[super viewDidLoad];
// Crashes here:
self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
I'm using ARC and my code seems to be entirely generic. I looked around and everywhere it's the same code for adding a map view. And yet it crashes with EXE_BAD_ACCESS. What am I missing?
My working code:
- (void)viewDidLoad
{
[super viewDidLoad];
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
[self.view addSubview:mapView];
}
Related
In my viewcontroller like this:
#import "PPSharedView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
PPSharedView * sharedView = [[PPSharedView alloc] init];
sharedView.topImageName = #"fenxiangdao";
[sharedView sharedWithArrayImage:#[#"weixin_um",#"weixin_um",#"weixin_um",] titles:#[#"微信好友",#"微信好友",#"微信好友",]];
}
#end
In PPSharedView.h like this:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface PPSharedView : UIView
-(void)sharedWithArrayImage:(nonnull NSArray *)arrImgs
titles:(nonnull NSArray *)arrTitles;
#end
In PPSharedView.m on the below:
-(void)sharedWithArrayImage:(NSArray *)arrImgs titles:(NSArray *)arrTitles{
if (arrImgs.count != arrTitles.count) return;
UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_W, kSCREEN_H)];
[keyWindow addSubview:backView];
backView.backgroundColor = [UIColor blackColor];
backView.alpha = 0.5;
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(clickRedview)];
backView.userInteractionEnabled = YES;
[backView addGestureRecognizer:tap];
}
-(void)clickRedview{
NSLog(#"adf");
}
When I run the app on the iPhone the backView on the screen but can't response click events.Anyone knows what's wrong with the code?Thank you very much!
In controller the sharedView should be referenced example:
#interface ViewController ()
#property(nonatomic, strong) PPSharedView * ppView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
PPSharedView * sharedView = [[PPSharedView alloc] init];
self.ppView = sharedView;
sharedView.topImageName = #"fenxiangdao";
[sharedView sharedWithArrayImage:#[#"weixin_um",#"weixin_um",#"weixin_um",] titles:#[#"微信好友",#"微信好友",#"微信好友",]];
}
#end
If don't do this the sharedView will be destoryed!
I've created a custom UIViewController with storyboard and this is the class:
.h file:
#import <UIKit/UIKit.h>
#interface PlayerViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *PlayerNumber;
#property (weak, nonatomic) IBOutlet UIImageView *PlayerImage;
#property (weak, nonatomic) IBOutlet UILabel *PlayerNameLabel;
-(void)setPlayerName:(NSString*)PlayerNameString;
#end
.m file:
#import "PlayerViewController.h"
#interface PlayerViewController ()
#end
#implementation PlayerViewController
#synthesize PlayerImage,PlayerNameLabel,PlayerNumber;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void)setPlayerName:(NSString *)PlayerNameString{
self.PlayerNameLabel.text=PlayerNameString;
}
#end
from main view I create 2 instance of PlayerViewController in order to display 2 custom views:
- (void)viewDidLoad {
[super viewDidLoad];
[self DrawPlayerWithTag:1];
[self DrawPlayerWithTag:2];
}
- (void)DrawPlayerWithTag:(int)PlayerTag{
PlayerViewController *myPlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PlayerViewController"];
UIView *myPlayerView=myPlayerViewController.view;
myPlayerView.tag=PlayerTag;
myPlayerView.frame=CGRectMake(0, 0, 100, 100);
myPlayerView.center = CGPointMake(self.view.frame.size.width / 2,
self.view.frame.size.height / 2);
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doDoubleTap)];
doubleTap.numberOfTapsRequired = 2;
[myPlayerView addGestureRecognizer:doubleTap];
[self.view addSubview:myPlayerView];
[myPlayerViewController setPlayerName:#"NameA"];
}
It seems to work.
But, if I want to change the PlayerName, how can refer to view1 rather then view2 after a double tap event, and use the method setPlayerName?
Thanks a lot for your answer!
Change the callback method
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doDoubleTap:)];
You can get the tag of the view like this:
-(void)doDoubleTap:(UITapGestureRecognizer *)gesture{
NSLog(#"%ld", gesture.view.tag);
}
You can keep your code and invoke setPlayerName with this post Get to UIViewController from UIView?
But, I suggest this way:
set target for gesture is myPlayerViewController
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:myPlayerViewController action:#selector(doDoubleTap:)];
Keep ref of myPlayerViewController in "main view" by an array like:
[arrPlayerView addObject:myPlayerViewController]; /// keep pointing to myPlayerViewController, so it isn't released.
Place (void)doDoubleTap:(UITapGestureRecognizer *)gesture in PlayerViewController
Following this tutorial on how to display XML latitude and longitude data as pins on iOS map kit:
http://highoncoding.com/Articles/805_Consuming_XML_Feed_and_Displaying_Public_Information_on_the_MapView_Control.aspx
The sample code provided compiles correctly and displays pins all across the united states. However when I tried to "port" the .xib into my app It pulls up my Mapview and the current users location, but it won't drop any pins/parse the data?
I'm on Day 2 now, its a bit discouraging.
Heres my .m and .h
EleventhViewController.h
// SlideMenu
//
// Created by Kyle Begeman on 1/13/13.
// Copyright (c) 2013 Indee Box LLC. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
//#interface EleventhViewController : NSObject <UIApplicationDelegate,MKMapViewDelegate,CLLocationManagerDelegate> {
#interface EleventhViewController : UIViewController <MKMapViewDelegate,CLLocationManagerDelegate,UIApplicationDelegate>
{
IBOutlet MKMapView *mapView;
NSMutableArray *greenCities;
CLLocationManager *locationManager;
}
//-(void) MKMapViewDelegate;
//-(void) CLLocationManagerDelegate;
//#property (nonatomic, weak) id<UIApplicationDelegate> delegate;
//#property (nonatomic, weak) id<MKMapViewDelegate> delegate;
//#property (nonatomic, weak) id<CLLocationManagerDelegate> delegate;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic,retain) IBOutlet MKMapView *mapView;
#property (nonatomic,retain) NSMutableArray *greenCities;
#property (strong, nonatomic) UIButton *menuBtn;
#property (strong, nonatomic) UIButton *searchBtn;
#end
Heres the .m
//
// EleventhViewController.m
// SlideMenu
//
// Created by Kyle Begeman on 1/13/13.
// Copyright (c) 2013 Indee Box LLC. All rights reserved.
//
#import "EleventhViewController.h"
#import "ECSlidingViewController.h"
#import "MenuViewController.h"
#import "GreenCitiesAppDelegate.h"
#import "GreenCitiesService.h"
#import "GreenCityAnnotation.h"
#import "GreenCityAnnotationView.h"
#import "GreenCity.h"
#interface EleventhViewController ()
//#interface EleventhViewController : UIViewController <MKMapViewDelegate>
#end
#implementation EleventhViewController
#synthesize window=_window,mapView,greenCities;
#synthesize menuBtn;
#synthesize searchBtn;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.mapView setShowsUserLocation:YES];
GreenCitiesService *greenService = [[GreenCitiesService alloc] init];
self.greenCities = [greenService getGreenCities];
[self.window makeKeyAndVisible];
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.window.layer.shadowOpacity = 0.75f;
self.window.layer.shadowRadius = 10.0f;
self.window.layer.shadowColor = [UIColor blackColor].CGColor;
if (![self.slidingViewController.underLeftViewController isKindOfClass:[MenuViewController class]]) {
self.slidingViewController.underLeftViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"Menu"];
}
self.menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
menuBtn.frame = CGRectMake(9, 23, 40, 30);
[menuBtn setBackgroundImage:[UIImage imageNamed:#"menuButton.png"] forState:UIControlStateNormal];
[menuBtn addTarget:self action:#selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:self.menuBtn];
//Top Main Menu Search Button
self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
searchBtn.frame = CGRectMake(275, 25, 40, 30);
[searchBtn setBackgroundImage:[UIImage imageNamed:#"searchButton.png"] forState:UIControlStateNormal];
[searchBtn addTarget:self action:#selector(revealMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:self.searchBtn];
}
- (void)mapView:(MKMapView *)mv didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"didUpdateUserLocation fired!");
CLLocationCoordinate2D maxCoord = {-90.0f,-180.0f};
CLLocationCoordinate2D minCoord = {90.0f, 180.0f};
for(int i = 0; i<=[self.greenCities count] - 1;i++)
{
GreenCity *gCity = (GreenCity *) [self.greenCities objectAtIndex:i];
CLLocationCoordinate2D newCoord = { gCity.latitude, gCity.longitude };
if(gCity.longitude > maxCoord.longitude)
{
maxCoord.longitude = gCity.longitude;
}
if(gCity.latitude > maxCoord.latitude)
{
maxCoord.latitude = gCity.latitude;
}
if(gCity.longitude < minCoord.longitude)
{
minCoord.longitude = gCity.longitude;
}
if(gCity.latitude < minCoord.latitude)
{
minCoord.latitude = gCity.latitude;
}
GreenCityAnnotation *annotation = [[GreenCityAnnotation alloc] initWithCoordinate:newCoord title:gCity.name subTitle:gCity.rank];
[mv addAnnotation:annotation];
// [annotation release];
}
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center.longitude = (minCoord.longitude + maxCoord.longitude) / 2.0;
region.center.latitude = (minCoord.latitude + maxCoord.latitude) / 2.0;
// calculate the span
region.span.longitudeDelta = maxCoord.longitude - minCoord.longitude;
region.span.latitudeDelta = maxCoord.latitude - minCoord.latitude;
[self.mapView setRegion:region animated:YES];
}
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)revealMenu:(id)sender
{
[self.slidingViewController anchorTopViewTo:ECRight];
}
#end
My Project is pretty simple, uses a lot of free source code so feel free to download what I've made up to this point, I'm pretty sure it would make a good template/starting base for a lot of you:
https://www.dropbox.com/s/8xxx08zyqpr9i8v/CDF.zip
The method didFinishLauching with options should only be used in the app delegate. You need to move the code from here into viewDidLoad and delete the didFinishLaunching method. Also delete the reference you have to the app delegate from the storyboard xib.
You really want to then set breakpoints in the app and work out where the data load is going wrong.
That fixed it! I moved every piece of executed code under the viewDidLoad! I feel like a noob (which I am) I would up vote you but I don't have enough rep yet. However I keep my connections to the app delegate I imported from the greencities .zip source. Thanks so much. This has however created a new bug in that I can't access my slider menu...fix one problem get another...I really appreciate the direction
Okay so i started learning UIDynamics and wanted to just work it out on small box.
I declared UIGravityBehaviour and UIDynamicsBehaviour in my ViewController.m and coded the UIView, but when i executed the code, it was not working.
But just as i declared them as #property in my .h file, the program was working completely fine.
What is the reason behind that?
What is the difference between declaring it as #property and without declaring it as #property?
Here are my files.
Before using #property
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
UIDynamicAnimator* myAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior* gravityBehaviour = [[UIGravityBehavior alloc] initWithItems:#[box]];
[myAnimator addBehavior: gravityBehaviour];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
After Declaring it as #property
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong,nonatomic) UIDynamicAnimator* myAnimator ;
#property (strong,nonatomic) UIGravityBehavior *gravityBehavior;
#property (strong,nonatomic) UICollisionBehavior *collisionBehavior ;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView* box = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
box.backgroundColor = [UIColor blackColor];
[self.view addSubview:box];
self.myAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:#[box]];
self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:#[box]];
self.collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.myAnimator addBehavior:self.gravityBehavior];
[self.myAnimator addBehavior:self.collisionBehavior];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You don't have to move UIGravityBehavior to a property, only UIDynamicAnimator to make it work. However, it's probably a good idea anyway in case you want to remove specific gravity behaviours rather than all of them at once.
If you are using ARC:
The UIDynamicAnimator probably does not have a strong reference to the UIGravityBehavior. So after your viewDidLoad method returns, no one owns that UIGravityBehavior and ARC will make sure that that point it is released. When you make a property for it, your viewController owns it, so the UIGravityBehavior will survive until you viewController gets deallocated.
I am building radio APP, I have added UIView to my ViewController and changed its class to MPVolumeView. The slider appears when I test it on my device but is non-responsive to touch gestures. What's weird is that it responds to hardware change with the volume buttons. Here is my code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController{
MPVolumeView *_mpVolumeView;
}
- (IBAction)playPressed:(id)sender;
#property (strong, nonatomic) IBOutlet MPVolumeView *mpVolumeView;
And the m. file:
#interface ViewController ()
{
AVPlayer *vPlayer;
}
#end
#implementation ViewController
#synthesize mpVolumeView = _mpVolumeView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *vibes = [NSURL URLWithString:#"http://vibesradio.org:8002/listen.pls"];
vPlayer = [[AVPlayer alloc] initWithURL:vibes];
_mpVolumeView.backgroundColor = [UIColor clearColor];
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: _mpVolumeView.bounds];
[_mpVolumeView addSubview:myVolumeView];
}
Thanks for any tips and suggestions to fix this issues. I have spent several hours trying to find a solution without any luck.
You may want to change:
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: _mpVolumeView.bounds];
[_mpVolumeView addSubview:myVolumeView];
towards:
self.myVolumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero];
[self.myVolumeView sizeToFit];
[self.view addSubview:self.myVolumeView];