iOS在不同的视图控制器上共享通用视图

时间:2014-07-28 08:31:47

标签: ios objective-c iphone ios7

我在iOS中实现了滑动菜单代码,并希望为所有视图控制器共享该单个代码。可能吗 ??如果是,那么我如何在我的项目中实现这一点? 任何人都可以帮助我吗?

这是我在视图控制器中编写的代码,我想为所有视图控制器共享此代码。

(void)initslidingcode
{


    //selected button background

    _selectedbtnback=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Menu_selection.png"]];

    //end

    //sliding code

    slidestatus=0;
    _menu=[[UIView alloc]initWithFrame:CGRectMake(-190,60,190,450)];
    UIImageView *back=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Menu_Bg.png"]];
    back.frame=CGRectMake(0,-20,220,450);
    [_menu insertSubview:back atIndex:0];

    _menu.userInteractionEnabled=NO;
    [_leftmenu setTarget:self];
    [_leftmenu setAction:@selector(showmenu)];

    UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(45,7,80,80)];

    img.image=[UIImage imageNamed:@"profile.jpg"];
    img.layer.cornerRadius=img.frame.size.width/2;
    img.clipsToBounds=YES;
    img.layer.borderWidth = 1.0f;
    img.layer.borderColor = [UIColor greenColor].CGColor;
    [_menu addSubview:img];


    UILabel *username=[[UILabel alloc] initWithFrame:CGRectMake(0,88,175,18)];
    username.text=@"Dilip Ingole";
    username.textColor=[UIColor whiteColor];
    username.textAlignment=NSTextAlignmentCenter;
    [_menu addSubview:username];



     UIImageView *line=[[UIImageView alloc] initWithFrame:CGRectMake(0,105,180,15)];
    line.image=[UIImage imageNamed:@"menu_divided_line.png"];
    [_menu addSubview:line];



    _activitybtn=[[UIButton alloc] initWithFrame:CGRectMake(0,110,180,37)];
    _inboxbtn=[[UIButton alloc] initWithFrame:CGRectMake(0,150,180,37)];
    _nutritionchartbtn=[[UIButton alloc] initWithFrame:CGRectMake(0,190,180,37)];
    _trainingplanbtn=[[UIButton alloc] initWithFrame:CGRectMake(0,230,180,37)];
    _heartbeatbtn=[[UIButton alloc] initWithFrame:CGRectMake(0,270,180,37)];
    _friends=[[UIButton alloc] initWithFrame:CGRectMake(0,310,180,37)];
    _goalsbtn=[[UIButton alloc] initWithFrame:CGRectMake(0,350,180,37)];
    _goshopbtn=[[UIButton alloc] initWithFrame:CGRectMake(0,390,180,37)];



    [_activitybtn setTitle:@"Activity" forState:UIControlStateNormal];
    _activitybtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [_activitybtn setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
    [_activitybtn addTarget:self action:@selector(openactivity) forControlEvents:UIControlEventTouchUpInside];
    img=[[UIImageView alloc] initWithFrame:CGRectMake(2, 1, 40, 40)];
    img.image=[UIImage imageNamed:@"Activity_icon.png"];
    _activitybtn.titleEdgeInsets = UIEdgeInsetsMake(0, 50, 0,0);
    [_activitybtn addSubview:img];
    [_menu addSubview:_activitybtn];

    UIImageView *line1=[[UIImageView alloc] initWithFrame:CGRectMake(0,145,180,15)];
    line1.image=[UIImage imageNamed:@"menu_divided_line.png"];
    [_menu addSubview:line1];


    [_inboxbtn setTitle:@"Inbox" forState:UIControlStateNormal];
    [_inboxbtn.titleLabel setTextAlignment:NSTextAlignmentLeft];
    [_inboxbtn setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
    [_inboxbtn addTarget:self action:@selector(openinbox) forControlEvents:UIControlEventTouchUpInside];
    img=[[UIImageView alloc] initWithFrame:CGRectMake(2, 1, 40, 40)];
    img.image=[UIImage imageNamed:@"Msgbox_icon.png"];
    _inboxbtn.titleEdgeInsets = UIEdgeInsetsMake(0,0,0,30);
    [_inboxbtn addSubview:img];
    [_menu addSubview:_inboxbtn];

    UIImageView *line2=[[UIImageView alloc] initWithFrame:CGRectMake(0,183,180,15)];
    line2.image=[UIImage imageNamed:@"menu_divided_line.png"];
    [_menu addSubview:line2];



    [_nutritionchartbtn setTitle:@"Nutrition Chart" forState:UIControlStateNormal];
    [_nutritionchartbtn.titleLabel setTextAlignment:NSTextAlignmentLeft];
    [_nutritionchartbtn setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
    [_nutritionchartbtn addTarget:self action:@selector(opennutritionchart) forControlEvents:UIControlEventTouchUpInside];
    img=[[UIImageView alloc] initWithFrame:CGRectMake(2, 1, 40, 40)];
    img.image=[UIImage imageNamed:@"Nutrition_Icon.png"];
    _nutritionchartbtn.titleEdgeInsets = UIEdgeInsetsMake(0,45, 0,0);
    [_nutritionchartbtn addSubview:img];
    [_menu addSubview:_nutritionchartbtn];

    UIImageView *line3=[[UIImageView alloc] initWithFrame:CGRectMake(0,223,180,15)];
    line3.image=[UIImage imageNamed:@"menu_divided_line.png"];
    [_menu addSubview:line3];




    [_trainingplanbtn setTitle:@"Training Plan" forState:UIControlStateNormal];
    [_trainingplanbtn setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
    [_trainingplanbtn addTarget:self action:@selector(opentrainingplan) forControlEvents:UIControlEventTouchUpInside];
    img=[[UIImageView alloc] initWithFrame:CGRectMake(2, 1, 40, 40)];
    img.image=[UIImage imageNamed:@"Training_icon.png"];
    _trainingplanbtn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0,0);
    [_trainingplanbtn addSubview:img];
    [_menu addSubview:_trainingplanbtn];

    UIImageView *line4=[[UIImageView alloc] initWithFrame:CGRectMake(0,263,180,15)];
    line4.image=[UIImage imageNamed:@"menu_divided_line.png"];
    [_menu addSubview:line4];



    [_heartbeatbtn setTitle:@"Heart Beat" forState:UIControlStateNormal];
    [_heartbeatbtn setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
    [_heartbeatbtn addTarget:self action:@selector(openheartbeat) forControlEvents:UIControlEventTouchUpInside];
    img=[[UIImageView alloc] initWithFrame:CGRectMake(2, 1, 40, 40)];
    img.image=[UIImage imageNamed:@"heart_icon.png"];
    _heartbeatbtn.titleEdgeInsets = UIEdgeInsetsMake(0,20, 0,0);
    [_heartbeatbtn addSubview:img];
    [_menu addSubview:_heartbeatbtn];

    UIImageView *line5=[[UIImageView alloc] initWithFrame:CGRectMake(0,303,180,15)];
    line5.image=[UIImage imageNamed:@"menu_divided_line.png"];
    [_menu addSubview:line5];



    [_friends setTitle:@"Friends" forState:UIControlStateNormal];
    [_friends setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
    [_friends addTarget:self action:@selector(openfriends) forControlEvents:UIControlEventTouchUpInside];
    img=[[UIImageView alloc] initWithFrame:CGRectMake(2, 1, 40, 40)];
    img.image=[UIImage imageNamed:@"Friends_icon.png"];
    _friends.titleEdgeInsets = UIEdgeInsetsMake(0,0, 0,5);
    [_friends addSubview:img];
    [_menu addSubview:_friends];

    UIImageView *line6=[[UIImageView alloc] initWithFrame:CGRectMake(0,343,180,15)];
    line6.image=[UIImage imageNamed:@"menu_divided_line.png"];
    [_menu addSubview:line6];



    [_goalsbtn setTitle:@"Goals" forState:UIControlStateNormal];
    [_goalsbtn setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
    [_goalsbtn addTarget:self action:@selector(opengoals) forControlEvents:UIControlEventTouchUpInside];
    img=[[UIImageView alloc] initWithFrame:CGRectMake(2, 1, 40, 40)];
    img.image=[UIImage imageNamed:@"Goals_icon.png"];
    _goalsbtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0,25);
    [_goalsbtn addSubview:img];
    [_menu addSubview:_goalsbtn];

    UIImageView *line7=[[UIImageView alloc] initWithFrame:CGRectMake(0,383,180,15)];
    line7.image=[UIImage imageNamed:@"menu_divided_line.png"];
    [_menu addSubview:line7];



    [_goshopbtn setTitle:@"Go Shop" forState:UIControlStateNormal];
    [_goshopbtn setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
    [_goshopbtn addTarget:self action:@selector(opengoshop) forControlEvents:UIControlEventTouchUpInside];
    img=[[UIImageView alloc] initWithFrame:CGRectMake(2, 1, 40, 40)];
    img.image=[UIImage imageNamed:@"Shoping Icon.png"];
    _goshopbtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0,0);
    [_goshopbtn addSubview:img];
    [_menu addSubview:_goshopbtn];


    //sliding


}
-(void)showmenu
{

    [_menu removeFromSuperview];
    _menu.userInteractionEnabled=YES;

    if (slidestatus==0)
    {

        slidestatus=1;
        [UIView animateWithDuration:0.5f animations:^{
            _menu.frame = CGRectMake(0,60,190,450);

        }];
    }
    else
    {
        slidestatus=0;
        [UIView animateWithDuration:0.5f animations:^{
            _menu.frame = CGRectMake(-190,60,190,450);
        }];
    }

    [self.view insertSubview:_menu atIndex:100];

}

-(void)openactivity
{

    //open activity controller

}

-(void)openinbox
{
    [_inboxbtn setImage:[UIImage imageNamed:@"Menu_selection.png"] forState:UIControlStateHighlighted];
        //open activity controller

}
-(void)opennutritionchart
{
    //open activity controller

}
-(void)opentrainingplan
{
    //open activity controller

}

-(void)openheartbeat
{
    //open activity controller

    NSLog(@"heart controller");

}
-(void)openfriends
{
    //open activity controller

}
-(void)opengoals
{
    //open activity controller

}
-(void)opengoshop
{
    //open activity controller

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    slidestatus=0;
    [UIView animateWithDuration:0.5f animations:^{
        _menu.frame = CGRectMake(-190,60,190,450);

    }];

}

1 个答案:

答案 0 :(得分:0)

这些家伙在旁边的菜单上做得非常好。

还有集成说明。

MVYSideMenu

无论如何,为了完成您的任务,请考虑从AppDelegate管理您的侧边菜单,并考虑在appDelegate.window上添加侧边菜单,例如UITabBarController