Question

created a class, GSSFeatureScreenLocationTabView : UIView, to add multiple view into a scroll view, the view comes up with buttons which are linked to a selector that is supposed to push to another view controller, It sends the placeID back up but from some reason doesn't push to the next view controller

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

in 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];
}

the function -(void) pushToLocationWithID:(NSString*)placeID is called and the log shows the function and placeID, However it doesn't push to the location view controller. tried creating a button just to link a segue to it and called the performSegue with identifier, doesn't reach prepare for segue and still doesn't push.

However using the same code from another page in an IBAction and it works. Is there someway to call super or something so it goes into the navController and pushes it ?

If i do presentViewController just to test modal like shown above it shows

Warning: Attempt to present <GSSLocationDetailViewController: 0xab85110> on <featureScreenViewController: 0xab9c230> whose view is not in the window hierarchy!

but if i do

    [self.navigationController pushViewController:vc animated:YES];

it doesn't show anything in the log and doesn't push.

made a new project with same concept at http://www.narula-tech.com/testScrollPush.zip

Was it helpful?

Solution

You are in a UIView (GSSFeatureScreenLocationTabView) and you are allocating a new viewController (VC2: featureScreenViewController) from there and you are pushing another viewController (VC3: GSSLocationDetailViewController) from there.

But actually your view is in another viewController VC1 and you need to push VC3 from there and not from VC2 which you are allocating but is not "in the scene" or better, in the window hierarchy.

So you should control touches from the viewController and not from the UIView..for this reason the name: view**CONTROLLER**.

Another thing but not related: name of class should began with the first letter capitol:

So not:

featureScreenViewController* fsvc;

but:

FeatureScreenViewController* fsvc;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top