Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

2.6.UIのセットアップ

hkato edited this page Mar 25, 2016 · 2 revisions

UIの初期化

java

// アクションボタン
Button btnAction = (Button) findViewById(R.id.btnAction);
btnAction.setEnabled(true);
btnAction.setOnClickListener(new View.OnClickListener(){
  @Override
  public void onClick(View v){
    v.setEnabled(false);
    if (!_bEstablished){
      getPeerList();
    }
    else{
      close();
    }
    v.setEnabled(true);
  }
});

Objective-C

略
UIButton* btnCall = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnCall setTag:TAG_WEBRTC_ACTION];
[btnCall setFrame:rcCall];
[btnCall setTitle:@"Call" forState:UIControlStateNormal];
[btnCall addTarget:self action:@selector(pushCallButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnCall];
略)
}
略)
- (void)pushCallButton:(NSObject *)sender
{
	UIButton* btn = (UIButton *)sender;
	
	if (TAG_WEBRTC_ACTION == btn.tag)
	{
		if (nil == _mediaConnection)
		{
			// Listing all peers
            [self getPeerList];
		}
		else
		{
			// Closing chat
			[self performSelectorInBackground:@selector(closeChat) withObject:nil];
		}
	}
	
}

Swift

//ボタンはstoryboardで設定

@IBAction func pushCallButton(sender: AnyObject) {
    if self._mediaConnection == nil {
        self.getPeerList()
    }else{
        self.performSelectorInBackground("closeChat", withObject: nil)
    }
}

UIの更新

java

// UIを更新する
private void updateUI() {
  _handler.post(new Runnable() {
    @Override
    public void run() {
      Button btnAction = (Button) findViewById(R.id.btnAction);
      if (null != btnAction) {
        if (false == _bEstablished) {
          btnAction.setText("Call");
        } else {
          btnAction.setText("Hang up");
        }
      }

      TextView tvOwnId = (TextView) findViewById(R.id.tvOwnId);
      if (null != tvOwnId) {
        if (null == _id) {
          tvOwnId.setText("");
        } else {
          tvOwnId.setText(_id);
        }
      }
    }
  });
}

Objective-C

-(void)updateUI{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        //update Call Button
        UIButton* btn = (UIButton *)[self.view viewWithTag:TAG_WEBRTC_ACTION];
        if (NO == _bEstablished)
        {
            [btn setTitle:@"Call" forState:UIControlStateNormal];
        }
        else
        {
            [btn setTitle:@"Hang up" forState:UIControlStateNormal];
        }

        //update ID Label
        UILabel* lbl = (UILabel*)[self.view viewWithTag:TAG_ID];
        if (nil == _id)
        {
            [lbl setText:@"your ID: "];
        }
        else
        {
            [lbl setText:[NSString stringWithFormat:@"your ID: \n%@", _id]];
        }
    });
}

Swift

func updateUI(){
    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        
        //CALLボタンのアップデート
        if self._bEstablished == false{
            self.callButton.titleLabel?.text = "CALL"
        }else{
            self.callButton.titleLabel?.text = "Hang up"
        }
        
        //IDラベルのアップデート
        if self._id == nil{
            self.idLabel.text = "your Id:"
        }else{
            self.idLabel.text = "your Id:"+self._id! as String
        }
    }
}