0

uiactionsheet 内に Uiswitch を実装しています。必要な場所に配置できましたが、使用する方法がありませんでした。ここに私のコードがあります:

switchButton = [[UISwitch alloc] init];
[mySheet addSubview:switchButton];
[mySheet showInView:self.view];

UILabel *instruction = [[UILabel alloc] init];
[instruction setBackgroundColor:[UIColor clearColor]];
[instruction setText:@"Daily"];
[mySheet addSubview:instruction];

// get the dimension of the sheet to position the switch
CGSize parentSize  = mySheet.frame.size;
CGRect rect = switchButton.frame;
rect.origin.x = parentSize.width/2.0 - rect.size.width + 90;
rect.origin.y = parentSize.height - rect.size.height + 90 ;
switchButton.frame = rect;


//get the dimension of the sheet to possition the label
rect.origin.x = parentSize.width/2.0 - rect.size.width -20;
rect.origin.y = parentSize.height - rect.size.height + 90 ;
instruction.frame = rect;

スイッチ ボタンを ioutlet として初期化しました。アクションシートの機能では、これをやろうとしています。

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:        (NSInteger)buttonIndex{



    switch (buttonIndex) {
        case 0:
        {

            NSLog(@"Yes Button is working");
        }
            break;
         case 1:
        {

            if(switchButton.on){

            NSLog(@"IS on ?");
        }
          else {

         NSLog(@"Is off?");

         }
        }
        default:
            break;
    }

それはすべて設定されています

**IMP 「-(ibAction)funct」を作成したものをさらに試してみましたが、これを行ったときに、これをスイッチにリンクすることができませんでした **

4

4 に答える 4

1

次のコードが役立ちます

switchButton = [[UISwitch alloc] init];

[switchButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside];
于 2013-03-19T14:43:11.680 に答える
0

プログラムでスイッチを作成しているため、IBOutletとして設定しても何も起こりません。実際には、単純なボタン選択用に設計されたUIActionSheetを使用するのではなく、モーダルに表示する独自のUIViewControllerを作成する(そしてInterface Builderで設計する)必要があります。

ただし、UIActionSheetを引き続き使用する場合は、UISwitchを作成するときに「addTarget」を呼び出して、スイッチが変更されたときにイベントをキャッチすることができます。

于 2013-03-19T14:39:05.593 に答える
0

サブビューを UIActionSheet に追加する前に、UISwitch ビューのタグ値 (たとえば 6) を設定することもできます。次に、メソッドclickedButtonAtIndexが起動されると、次を使用してその値を確認できます[(UISwitch *)[actionSheet viewWithTag:6] isChecked]

于 2013-03-19T14:47:18.387 に答える
0

の値を設定できる BOOL フラグを持つことができます。UISwitch

[mySwitch addTarget:self action:@selector(switchChanged) forControlEvents: UIControlEventTouchUpInside];
-(void)switchChanged{
    if(switchOn){
     switchOn=NO;
   }
   else {
     switch=YES;
   }
}

フラグActionSheetを確認できますswithcOn

于 2013-03-19T14:49:59.990 に答える