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

时间:2014-05-05 11:22:14

标签: ios uiview uiviewcontroller navigation scrollview

创建了一个类, 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中其他页面的相同代码并且它可以正常工作。有没有什么可以调用超级或其他东西,所以它进入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

创建了一个具有相同概念的新项目

1 个答案:

答案 0 :(得分:1)

您位于UIViewGSSFeatureScreenLocationTabView),并且您正在从那里分配新的viewController VC2 featureScreenViewController)从那里推送另一个viewController VC3 GSSLocationDetailViewController)。

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

所以你应该控制来自viewController的触摸,而不是来自UIView ..因为这个名字:view ** CONTROLLER **。

另一件事但不相关: 课程名称应以首字母国会大厦开头:

所以不是:

featureScreenViewController* fsvc;

但:

FeatureScreenViewController* fsvc;