【发布时间】:2014-05-05 11:22:14
【问题描述】:
创建了一个类, GSSFeatureScreenLocationTabView:UIView, 将多个视图添加到滚动视图中, 视图带有链接到选择器的按钮,该选择器应该推送到另一个视图控制器,它会发送 placeID 备份,但由于某种原因不会推送到下一个视图控制器
GSSFeatureScreenLocationTabView.h
#import "featureScreenViewController.h"
@interface GSSFeatureScreenLocationTabView : UIView
{
UIImageView* imageView;
UILabel* titleLabel;
UILabel* distanceLabel;
UILabel* descriptionLabel;
UIButton* pushButton;
}
@property (nonatomic) NSString* placeID;
-(void)setLocationInfo:(NSString*)ID title:(NSString*)title distance:(double)distance description:(NSString*)description imageURL:(NSString*)imageURL;
@end
GSSFeatureScreenLocationTabView.m
#import "GSSFeatureScreenLocationTabView.h"
#import "GSSLocationDetailViewController.h"
@implementation GSSFeatureScreenLocationTabView
@synthesize placeID = _placeID;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 144, 140)];
titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 148, 107, 21)];
distanceLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 177, 107, 21)];
descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 206, 107, 91)];
pushButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 144, 317)];
[titleLabel setText:@""];
[distanceLabel setText:@""];
[descriptionLabel setText:@""];
[descriptionLabel setLineBreakMode:NSLineBreakByWordWrapping];
[pushButton setTitle:@"" forState:UIControlStateNormal];
[pushButton addTarget:self action:@selector(pushToLocation) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:imageView];
[self addSubview:titleLabel];
[self addSubview:distanceLabel];
[self addSubview:descriptionLabel];
[self addSubview:pushButton];
}
return self;
}
-(void)setLocationInfo:(NSString *)ID title:(NSString *)title distance:(double)distance description:(NSString *)description imageURL:(NSString *)imageURL
{
_placeID = ID;
[titleLabel setText:title];
[distanceLabel setText:[NSString stringWithFormat:@"%f", distance]];
[descriptionLabel setText:description];
UIImage* image = [UIImage imageNamed:imageURL];
[imageView setImage:image];
}
-(void) pushToLocation
{
NSLog(@"%s", __FUNCTION__);
featureScreenViewController* fsvc = [[featureScreenViewController alloc]init];
[fsvc pushToLocationWithID:_placeID];
}
@end
在 featureScreenViewController.m 中
- (void)viewDidLoad
{
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
if (isiPhone5)
{
// this is iphone 4 inch
[scroller setFrame:CGRectMake(0, 251, 320, 317)];
}
else
{
[scroller setFrame:CGRectMake(0, 251, 320, 229)];
}
[self addLocationViews];
}
-(void)addLocationViews
{
int count = (int)[_placeArray count];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(152*count, 317)];
for (int i = 0; i<count; i++) {
_placeObject = [_placeArray objectAtIndex:i];
GSSFeatureScreenLocationTabView * view = [[GSSFeatureScreenLocationTabView alloc]initWithFrame:CGRectMake((152*i), 0, 144, 317)];
[view setLocationInfo:[_placeObject placeID] title:[_placeObject placeName] distance:[_placeObject distacne] description:@"description" imageURL:@"noImage.png"];
[scroller addSubview:view];
}
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = YES;
}
-(void) pushToLocationWithID:(NSString*)placeID
{
NSLog(@"\n%s \nplaceID:%@\n\n", __FUNCTION__, placeID);
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
GSSLocationDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"GSSLocationDetailViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
函数 -(void) pushToLocationWithID:(NSString*)placeID 被调用并且日志显示函数和placeID,但是它不会推送到位置视图控制器。 尝试创建一个按钮只是为了将一个 segue 链接到它并调用带有标识符的 performSegue,没有达到为 segue 做准备并且仍然没有推送。
但是,在 IBAction 中使用来自另一个页面的相同代码并且它可以工作。有什么办法可以调用 super 或其他东西,让它进入 navController 并推送它吗?
如果我做 presentViewController 只是为了测试上面显示的模式
Warning: Attempt to present <GSSLocationDetailViewController: 0xab85110> on <featureScreenViewController: 0xab9c230> whose view is not in the window hierarchy!
但如果我这样做了
[self.navigationController pushViewController:vc animated:YES];
它不会在日志中显示任何内容,也不会推送。
在http://www.narula-tech.com/testScrollPush.zip创建了一个具有相同概念的新项目
【问题讨论】:
-
如果你想从一个视图控制器呈现,那个ViewController的视图应该在窗口中。
-
我想推它,试了现在看看它是否有效。
标签: ios uiview uiviewcontroller navigation scrollview