【问题标题】:push to vc from UIView class under a view controller从视图控制器下的 UIView 类推送到 vc
【发布时间】: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


【解决方案1】:

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

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

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

另一件事但不相关: 类名应以首字母大写开头:

所以不是:

featureScreenViewController* fsvc;

但是:

FeatureScreenViewController* fsvc;

【讨论】:

  • 我用一个小写字母声明了该类,因此它的 featureScreenViewController (其余部分以大写字母开头,忘记为这个 o.O 按 shift)...我在 VC1,它有多个视图一个滚动视图,试图从那里到达 VC2。
  • 我也这样做 [super pushToViewController:vc]; ?在uiview文件中?
  • 不,您应该从 viewController 而不是从 UIView 控制触摸。因此名称:viewCONTROLLER
  • 那么,我在控制器本身中生成视图?感谢您耐心等待。我对此很不满意
  • 您的 viewContoller 有一个视图,您可以在其中添加更多视图。有很多方法可以控制这方面,但可以肯定的是,您不能从一个视图推送视图控制器,而只能从另一个视图控制器推送。出于这个原因,您应该从您的 viewController 控制视图。我不知道您在哪里生成视图,这是您的选择。可能是新手,您无法使用最好的架构,但您会改进。您的代码中的问题很大,因为您正在分配一个 viewController,但没有显示该 viewController。
猜你喜欢
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
相关资源
最近更新 更多