在 XCode 中子类化标签栏的问题

Posted

技术标签:

【中文标题】在 XCode 中子类化标签栏的问题【英文标题】:problems subclassing a tabbar in XCode 【发布时间】:2014-04-04 09:54:43 【问题描述】:

我正在寻找一个凸起的中心标签栏,并且已经看到了示例,但是在子类化时我无法让它工作?我一定是在某个地方遗漏了什么!?!?!我在代码 [self viewControllerWithTabTitle:@"Kitchens"], 中遇到错误('HomeViewController' 没有可见的@interface 声明选择器'viewControllerWithTabTitle;')和 [self addCenterButtonWithImage:[ UIImage imageNamed:@"reversedlogo.png"]];('HomeViewController' 没有可见的@interface 声明选择器'addCentreButtonWithImage;')

请有人指出我哪里出错了。

谢谢

子类是基础视图控制器:

.m

@implementation BaseViewController

// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image

    UIViewController* viewController = [[UIViewController alloc] init];
    viewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image tag:0];
    return viewController;


// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage

    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
    button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];

    CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
    if (heightDifference < 0)
        button.center = self.tabBar.center;
    else
    
        CGPoint center = self.tabBar.center;
        center.y = center.y - heightDifference/2.0;
        button.center = center;
    

    [self.view addSubview:button];


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

    return YES;


@end

.h

@interface BaseViewController : UITabBarController



// Create a view controller and setup it's tab bar item with a title and image
-(UIViewController*) viewControllerWithTabTitle:(NSString*)title image:(UIImage*)image;

// Create a custom UIButton and add it to the center of our tab bar
-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage;

@end

那么我的主视图是:

.m

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;



- (void)viewDidLoad 
    [super viewDidLoad];
    [self createStoneData];

    self.viewControllers = [NSArray arrayWithObjects:
                            **[self viewControllerWithTabTitle:@"Kitchens"],** (no visible @interface for 'HomeViewController' declares the selector 'viewControllerWithTabTitle;')
                            [self viewControllerWithTabTitle:@"Studies"],
                            [self viewControllerWithTabTitle:@"Libraries"],
                            [self viewControllerWithTabTitle:@"" image:nil],
                            [self viewControllerWithTabTitle:@"Dressing Rooms"],
                            [self viewControllerWithTabTitle:@"Bathrooms"],
                            [self viewControllerWithTabTitle:@"Contact Us"],



-(void)willAppearIn:(UINavigationController *)navigationController
    
        **[self addCenterButtonWithImage:[UIImage imageNamed:@"reversedlogo.png"]];** (no visible @interface for 'HomeViewController' declares the selector 'addCentreButtonWithImage;')
    




- (void)createStoneData 

    self.homeImages = [NSMutableArray array];

    [self.homeImages addObject:[[NSMutableDictionary alloc]
                                initWithObjectsAndKeys:@"Biddlesdon Wardrobes", @"name",
                                @"_NP19981-Edit.jpg", @"image", nil]];
    [self.homeImages addObject:[[NSMutableDictionary alloc]
                                    initWithObjectsAndKeys:@"South Stoke Bathroom", @"name",
                                    @"_NP17744.jpg", @"image", nil]];

    [self.collectionView reloadData];



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
    return self.homeImages.count;



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *homeImageView = (UIImageView *)[cell viewWithTag:100];
    homeImageView.image = [UIImage imageNamed:[[self.homeImages objectAtIndex:indexPath.row] objectForKey:@"image"]];

    return cell;


- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
    if (_startingIndexPath) 
        NSInteger currentIndex = floor((scrollView.contentOffset.x - scrollView.bounds.size.width / 1) / scrollView.bounds.size.width) + 1;
        if (currentIndex < [self.homeImages count]) 
            self.title = self.homeImages[currentIndex][@"name"];
        
    


- (BOOL) shouldAutorotate

    return YES;


-(NSUInteger)supportedInterfaceOrientations

    return UIInterfaceOrientationMaskAll;


- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

    [self.collectionView.collectionViewLayout invalidateLayout];


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


@end

.h

#import "BaseViewController.h"

@interface HomeViewController : BaseViewController 

@property (strong, nonatomic) NSMutableArray *homeImages;

@property (nonatomic, strong) NSIndexPath *startingIndexPath;

@property (nonatomic, strong) IBOutlet UICollectionView *collectionView;

- (void)createStoneData;


@end

【问题讨论】:

你的问题是什么? 查看关于错误的编辑 如果你从互联网上复制代码,你应该确保它们是完整的,你不应该把它们当作你的。 【参考方案1】:

这完全正常,因为您没有创建指定的方法。

让我们看看你的BaseViewController.h,你有两种方法

-(void) addCenterButtonWithImage:(UIImage*)buttonImage highlightImage:(UIImage*)highlightImage

-(UIViewController*) viewControllerWithTabTitle:(NSString*) title image:(UIImage*)image

你看到声明的方法有两个参数,但是你用一个参数调用它们。 喜欢,

[self viewControllerWithTabTitle:@"Kitchens"] 

它期望第二个参数为 UIImage

由于参数是方法名称的一部分,您会得到no visible interface错误。因为您的基类中没有任何方法名为:

 -(UIViewController*) viewControllerWithTabTitle:(NSString*) title;

【讨论】:

太棒了,谢谢,已经修改了,但是 addCenterButtonWithImage 错误也让我感到困惑,因为我确实有这个? 你也必须对 addCenterButtonWithImage 做同样的事情,因为你用一个参数调用它。 agh 这只是没有更新,但在我尝试运行时已清除,它已经崩溃,断点是 viewControllerWithTabTitle 部分末尾的 。我正在使用情节提要,我是否需要完全删除标签栏?抱歉,这完全是新手!

以上是关于在 XCode 中子类化标签栏的问题的主要内容,如果未能解决你的问题,请参考以下文章

XCTest Objective C Xcode8.1如何提取任何按钮或导航栏的标签/值/名称

带有标签栏的左侧菜单?

如何在 SwiftUI 的标签栏的特定视图中隐藏导航栏?

从ios中的UITabBarController中删除标签栏的安全方法

Xcode - 标签栏项目视图中的标签出口为零

标签栏和视图大小 xcode