从视图控制器下的UIView类推送到vc



创建一个类,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:%@nn", __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与标识符,没有达到prepareforsegue,仍然不推送。

然而,在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

您在UIView (GSSFeatureScreenLocationTabView)中,您正在从那里分配新的viewController (VC2: featureScreenViewController),并且您正在从那里推送另一个viewController (VC3: GSSLocationDetailViewController)。

但实际上你的视图是在另一个viewController VC1,你需要从那里推送VC3,而不是从VC2,你正在分配,但不是"在场景中"或更好,在窗口层次结构。

所以你应该控制触摸从viewController而不是从UIView..因此名称:view**CONTROLLER**.

另一件事但不相关:类名应以首字母capitol:

开头

:

featureScreenViewController* fsvc;

但:

FeatureScreenViewController* fsvc;

最新更新