【问题标题】:Push View Controller with disclosure button, receive black screen使用显示按钮推送视图控制器,接收黑屏
【发布时间】:2014-04-07 14:44:58
【问题描述】:

当在注释的标注中按下详细信息披露按钮时,我正在尝试按下 DetailViewController。但是,当我被定向到 DetailViewController 时,我只收到黑屏而不是我设置的标签。黑屏是什么原因?这是我的第一个 ViewController 的代码。

#import "ViewController.h"
#import "DetailViewController.h"
#import "Annotation.h"

#import "City.h"

@interface ViewController ()
@property (nonatomic, strong) IBOutlet DetailViewController *detailViewController;


@end
#define getDatalURL @"http://www.club-hop.com/apptest.php"

@implementation ViewController

@synthesize mapView,jsonArray,citiesArray;



- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveData];
self.detailViewController = [[DetailViewController alloc] init];
/* Zoom the map to current location.
[self.mapView setShowsUserLocation:YES];
[self.mapView setUserInteractionEnabled:YES];
[self.mapView setUserTrackingMode:MKUserTrackingModeFollow];*/
City * cityObject;

// load external page into UIWebView
NSMutableArray * locations= [[NSMutableArray alloc]init];
CLLocationCoordinate2D location;
Annotation * myAnn;

for(int u=0; u<citiesArray.count;u++){
cityObject=[citiesArray objectAtIndex:u];

myAnn=[[Annotation alloc]init];
NSNumber *aLat= cityObject.Latitude;
NSNumber *aLon= cityObject.Longitude;

double lat = [aLat doubleValue];
double lon = [aLon doubleValue];

location.latitude= lat;
location.longitude=lon;
myAnn.coordinate = location;
myAnn.title=cityObject.clubName;
myAnn.subtitle=cityObject.cityName;
[locations addObject:myAnn];}

[self.mapView addAnnotations:locations];


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}



//class methods
-(void) retrieveData{
NSURL * url= [NSURL URLWithString:getDatalURL];
NSData * data= [NSData dataWithContentsOfURL:url];
jsonArray= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//setup cities array
citiesArray=[[NSMutableArray alloc]init];

for(int i=0; i<jsonArray.count;i++){
    NSString * cID= [[jsonArray objectAtIndex:i] objectForKey:@"id"];
    NSString * cName= [[jsonArray objectAtIndex:i] objectForKey:@"cityName"];
    NSString * cCountry= [[jsonArray objectAtIndex:i] objectForKey:@"cityCountry"];
    NSString * cLine= [[jsonArray objectAtIndex:i] objectForKey:@"clubLine"];
    NSString * clName= [[jsonArray objectAtIndex:i] objectForKey:@"clubName"];
    NSNumber * cLatitude= [[jsonArray objectAtIndex:i] objectForKey:@"Latitude"];
    NSNumber * cLongitude= [[jsonArray objectAtIndex:i] objectForKey:@"Longitude"];

    [citiesArray addObject:[[City alloc]initWithCityName:cName andCityCountry:cCountry   andClubName:clName andClubLine:cLine andLatitude:cLatitude andLongitude:cLongitude andCityId:cID]];

}

}

#pragma mark - MKMapViewDelegate

// user tapped the disclosure button in the callout
//
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view   calloutAccessoryControlTapped:(UIControl *)control
{
 [self.navigationController pushViewController:self.detailViewController animated:YES];

}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id   <MKAnnotation>)annotation
{

MKPinAnnotationView* pinView = (MKPinAnnotationView *)[mapView   dequeueReusableAnnotationViewWithIdentifier:@"pinView"];

if (!pinView)
{
    pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation    reuseIdentifier:@"pinView"] ;
    pinView.pinColor=MKPinAnnotationColorGreen;
    pinView.animatesDrop=YES;
    pinView.canShowCallout=YES;
    UIButton * rightButton= [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView=rightButton;
}
else{
    pinView.annotation=annotation;
}
return pinView;

    }


@end

详细控制器文件

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController


- (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.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

  • 你能分享DetailViewController代码吗?
  • 您的电话[[DetailViewController alloc] init](就像我之前告诉过您的那样)您是否在DetailViewController 中覆盖了此方法?我们需要查看那个文件
  • 没有添加任何代码,它只是一个带有图像的 ViewController。但是,按下公开按钮后图像不可见。
  • ?如果您没有代码,那么您是如何将这些东西添加到其中的?
  • 请注意,在给某人发表评论时请使用“@”,以便他们收到您的评论

标签: ios objective-c uiviewcontroller uinavigationcontroller


【解决方案1】:

好吧,你似乎不明白我在问什么,所以我猜测你使用 .xib 文件来添加 UI 内容?

如果是这样,你需要使用这个:

[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]

NIBName 更改为你的。

编辑

要使用情节提要执行此操作,您需要给 viewController 一个情节提要并手动初始化它。按照此处列出的步骤操作:http://averagepro.com/2013/02/07/instantiate-storyboard-viewcontrollers-manually/

大致如下:

self.detailViewController = [[self.storyboard storyboardWithName:@"storyboard"bundle:nil] instantiateViewControllerWithIdentifier:@"DetailViewController"];

【讨论】:

  • 我使用的是故事板而不是 .xib 文件
  • @user3492592 要做到这一点,你需要添加一个storyboardID,然后调用它来初始化它,按照这个教程:averagepro.com/2013/02/07/…
  • @user3492592 你通常会使用带有故事板的 segue,但这种方法更类似于你以前使用的方法。我建议你阅读一些关于它们的教程,否则你会让自己变得更加困难
  • 在 xcode 的身份选项卡下,我只有“恢复 ID”可见,而不是“StoryBoard ID”和“使用 StoryBoard ID”,就像教程中一样..
  • 抱歉取消最后一条评论我已经想通了..再次感谢
【解决方案2】:

你需要用视图初始化你的DetailViewController

例如,如果接口是 .xib,请使用以下内容声明并初始化您的 DetailViewController

self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

如果是情节提要,请使用您在情节提要中附加到 DetailViewController 的标识符:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"StoryboardName"
                                          bundle:nil];
self.detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"DetailViewControllerID"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 2020-07-01
    • 2012-01-23
    相关资源
    最近更新 更多