【问题标题】:How to Navigate UIViewController as target of UIButton action created in programmatically如何导航 UIViewController 作为以编程方式创建的 UIButton 操作的目标
【发布时间】:2014-03-12 05:24:41
【问题描述】:

我正在以编程方式创建 UIButton。我希望 UIViewController 成为该按钮操作的目标。我该怎么做?如果我单击按钮导航将不起作用。这是我的代码。请帮助我任何人。谢谢提前。

- (void)viewDidLoad
{
    weeklyAudio=[UIButton buttonWithType:UIButtonTypeCustom];
    [weeklyAudio addTarget:self action:@selector(weeklyPredictions) forControlEvents:UIControlEventTouchUpInside];
    [weeklyAudio setTitle:@"WEEKLY PREDICTIONS" forState:UIControlStateNormal];
    weeklyAudio.titleLabel.textAlignment = NSLineBreakByWordWrapping;            
    weeklyAudio.titleLabel.numberOfLines = 2;
    [weeklyAudio.titleLabel setTextAlignment: NSTextAlignmentCenter];
    [weeklyAudio setTitleEdgeInsets:UIEdgeInsetsMake(60, 10, 10, 10)];
    [weeklyAudio.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
    weeklyAudio.frame=CGRectMake(10, 20, 140, 120);
    weeklyAudio.layer.cornerRadius=5;
    weeklyAudio.backgroundColor=[UIColor colorWithRed:232.0f/255.0f green:52.0f/255.0f blue:27.0f/255.0f alpha:1.0];

    [backGroundImage addSubview:weeklyAudio];

    weeklyAudioImage=[[UIImageView alloc]initWithFrame:CGRectMake(45, 10, 48, 48)];
    weeklyAudioImage.image=[UIImage imageNamed:@"m48*48.png"];
    [weeklyAudio addSubview:weeklyAudioImage];
    weeklyAudio.autoresizingMask =  UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleBottomMargin  | UIViewAutoresizingFlexibleTopMargin;
}

-(void)weeklyPredictions
{
    WeeklyViewController *weekly=[[WeeklyViewController alloc]init];
    [self.navigationController pushViewController:weekly animated:NO];
}

【问题讨论】:

  • “如果我单击按钮导航将不起作用”是什么意思?当你点击按钮时会发生什么?您还说您“希望 UIViewController 成为该按钮的目标”——什么视图控制器?这段代码所在的那个,还是其他控制器?
  • 如果你使用 .xib 文件,那么你有这样的定义 WeeklyViewController *weekly=[[WeeklyViewController alloc] initWithNibName:@"WeeklyViewController" bundle:nil];或者如果你使用故事板,那么就像这样 WeeklyViewController *weekly= [self.storyboard instantiateViewControllerWithIdentifier:@"WeeklyViewController"];
  • @Maul 感谢您的响应。我正在以编程方式创建。我没有使用 xib 和故事板
  • 您还没有回答有关拥有导航控制器的问题。如果你把这个日志,NSLog(@"%@", self.navigationController) 放在weeklyPredictions 方法中,它会给你什么?

标签: ios uiviewcontroller uibutton


【解决方案1】:

-(void)weeklyPredictions中设置断点,然后在点击按钮的时候调试看看是否在方法中运行。如果在其中运行,请在控制台中打印 po [self navigationController] 以查看 viewController 是否具有导航控制器。如果结果为 nil,则不能推送另一个视图控制器。

【讨论】:

    【解决方案2】:

    addSubview之前添加如下代码

    [weeklyAudio addTarget:self action:@selector(weeklyPredictions) forControlEvents:UIControlEventTouchUpInside]; 
    

    并将您的 weeklyPredictions 方法编辑为:

    -(void)weeklyPredictions {
        WeeklyViewController *weekly=[[WeeklyViewController alloc]initWithNibName:NSStringFromClass([WeeklyViewController class]) bundle:nil];
        [self.navigationController pushViewController:weekly animated:NO];
    }
    

    【讨论】:

    • OP 已经有将目标添加到按钮的行。并且更改 WeeklyViewController 的初始化方式可能是合适的,它与问题无关。
    【解决方案3】:

    您是否使用导航控制器来推送和弹出视图控制器?如果不.. 使用

    [self presentViewController:weekly animated:YES completion:nil];
    

    展示您的下一个视图控制器。

    【讨论】:

      【解决方案4】:

      您需要在 viewDidLoad 薄荷醇中添加以下行:

      backGroundImage.userInteractionEnabled = YES;
      

      由于默认情况下 UIImageView 禁用了用户交互,因此添加到图像视图的任何子视图都不会获得触摸事件。

      希望它能解决您的问题,如果是,请投票。

      问候, 萨滕德拉

      【讨论】:

      • 什么?他正在使用一个按钮。这与问题无关。
      猜你喜欢
      • 2013-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      相关资源
      最近更新 更多