在程序启动后想让按钮处于被选中状态?!

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    ViewController1 *v1=[[ViewController1 alloc]init];
    UINavigationController *navation=[[UINavigationController alloc]initWithRootViewController:v1];
    navation.title=@"首页";
    ViewController2 *v2=[[ViewController2 alloc]init];
    UITabBarItem *item2=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1];
    v2.tabBarItem=item2;
   
     ViewController3 *v3=[[ViewController3 alloc]init];
      UITabBarItem *item3=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:2];
    v3.tabBarItem=item3;
    NSArray *viewcontrols=[[NSArray alloc]initWithObjects:navation,v2,v3,nil];
    UITabBarController *tabcotrollers=[[UITabBarController alloc]init];
  
    tabcotrollers.selectedViewController=v2;
    tabcotrollers.tabBar.tintColor=[UIColor greenColor];
    tabcotrollers.viewControllers=viewcontrols;
    self.window.rootViewController=tabcotrollers;

错误如下:


Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITabBarController setSelectedViewController:] only a view controller in the tab bar controller's list of view controllers can be selected.'
*** First throw call stack:
(
0   CoreFoundation                      0x017ee1e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x0156d8e5 objc_exception_throw + 44
2   CoreFoundation                      0x017edfbb +[NSException raise:format:] + 139
3   UIKit                               0x0037facc -[UITabBarController setSelectedViewController:] + 180
4   UItabBarItem                        0x00002d39 -[AppDelegate application:didFinishLaunchingWithOptions:] + 1241
5   UIKit                               0x0022714f -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
6   UIKit                               0x00227aa1 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1810
7   UIKit                               0x0022c667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
8   UIKit                               0x00240f92 -[UIApplication handleEvent:withNewEvent:] + 3517
9   UIKit                               0x00241555 -[UIApplication sendEvent:] + 85
10  UIKit                               0x0022e250 _UIApplicationHandleEvent + 683
11  GraphicsServices                    0x037e3f02 _PurpleEventCallback + 776
12  GraphicsServices                    0x037e3a0d PurpleEventCallback + 46
13  CoreFoundation                      0x01769ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
14  CoreFoundation                      0x017699db __CFRunLoopDoSource1 + 523
15  CoreFoundation                      0x0179468c __CFRunLoopRun + 2156
16  CoreFoundation                      0x017939d3 CFRunLoopRunSpecific + 467
17  CoreFoundation                      0x017937eb CFRunLoopRunInMode + 123
18  UIKit                               0x0022bd9c -[UIApplication _run] + 840
19  UIKit                               0x0022df9b UIApplicationMain + 1225
20  UItabBarItem                        0x000037cd main + 141
21  libdyld.dylib                       0x01e35701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这个该怎么破?(xcode5.1 ios7的配置)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用QButtonGroup类来实现这一功能,该类可以将一组按钮视为单个实体并确保在同一时间只有一个按钮处于选中状态。以下是使用QButtonGroup实现此功能的示例代码: ```c++ #include <QApplication> #include <QWidget> #include <QRadioButton> #include <QButtonGroup> int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget *widget = new QWidget(); widget->setWindowTitle("Radio Buttons"); QRadioButton *radioButton1 = new QRadioButton("Radio Button 1", widget); QRadioButton *radioButton2 = new QRadioButton("Radio Button 2", widget); QRadioButton *radioButton3 = new QRadioButton("Radio Button 3", widget); QButtonGroup *buttonGroup = new QButtonGroup(widget); buttonGroup->addButton(radioButton1); buttonGroup->addButton(radioButton2); buttonGroup->addButton(radioButton3); QObject::connect(radioButton1, &QRadioButton::toggled, [=](bool checked) { if (checked) { radioButton2->setEnabled(true); radioButton3->setEnabled(true); } else { radioButton2->setEnabled(false); radioButton3->setEnabled(false); } }); radioButton2->setEnabled(false); radioButton3->setEnabled(false); widget->show(); return a.exec(); } ``` 在这个例子中,我们首先创建了一个QWidget对象,然后创建了三个QRadioButton对象,并将它们添加到QWidget对象中。然后,我们创建了一个QButtonGroup对象,并将三个QRadioButton对象添加到该组中。 接下来,我们使用连接函数(connect)将radioButton1的toggled信号连接到一个lambda表达式,该表达式在按钮选中时启用radioButton2和radioButton3,并在按钮选中时禁用它们。 最后,我们将radioButton2和radioButton3的setEnabled方法设置为false,以确保在应用程序启动时不会选择这些按钮。 当你运行这个应用程序选中radioButton1时,radioButton2和radioButton3将变为可选。如果你取消选中radioButton1,则这些按钮将变为不可选。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值