简体   繁体   中英

InitWithCoder called multiple times

I defined a custom initWithCoder method because I'm trying to make custom tool bar items. I have 5 view controllers in my storyboard, each with a custom class. They are all child classes of the initial view controller's class. The main problem is that instead of just changing the image of one classes tab bar item, it changes the image of each tab bar item that is pressed. Also, the images aren't changed to the unselected tab bar item image when the tab bar item is unselected. Each child classes view controller receives the same init method. My main problem is how do I make it so that each class, regardless of child or parent, receives a different init method?

 -(id)initWithCoder:(NSCoder *)aDecoder

{
        NSLog(@"First");
        if (self = [super initWithCoder:aDecoder]) 

    {
            NSLog(@"Second");
            [[self tabBarItem] setFinishedSelectedImage:[UIImage imageNamed:@"RedStar2.png"] 
            withFinishedUnselectedImage:[UIImage imageNamed:@"Star3.png"]];
            //instead of just changing the image of the class's tab bar item, it changes the image of all
            classes tab bar items
            [[self tabBarItem] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor
            redColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
      }
        return self;
} 

The keywords "first" and "second" are both called 5 times when the application is run. Each view controller has a custom class.

如果要为每个选项卡使用不同的图像,则必须在每个子类中将此方法重复5次,而不是在“公共祖先”(超类)中执行相同的工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM