CAAlertView(提示框)

2018-08-29 16:26 更新

類說明

CAAlertView是提示框控件,如果提示框內(nèi)的按鈕個(gè)數(shù)不超過三個(gè),這個(gè)橫向排列按鈕,如果按鈕個(gè)數(shù)超過三個(gè)則縱向排列。


CAAlertView 方法 (點(diǎn)擊方法名可查看方法介紹)

方法說明
addButton添加一個(gè)按鈕到CAAlertView
setAlertMessage提示框的提示信息 
hide隱藏提示框
setMessageFontName提示信息的字體 
show顯示提示框
setTarget添加監(jiān)聽 
setTitle提示框的標(biāo)題
hideWithDisplayed隱藏提示框
create創(chuàng)建
createWithText創(chuàng)建,并指定其Text
initWithText初始化,并指定化文本
addButton添加按鈕到CAAlertView


CAAlertView是提示框控件,如果提示框內(nèi)的按鈕個(gè)數(shù)不超過三個(gè),這個(gè)橫向排列按鈕,如果按鈕個(gè)數(shù)超過三個(gè)則縱向排列。


我們來看一下實(shí)例代碼:
首先在.h文件中聲明一下函數(shù):

//按鈕的回調(diào)函數(shù)
    void respondTouch(CAControl* btn, DPoint point);
 
//提示框的回調(diào)函數(shù)
    void alertViewCallback(int btnIndex);

然后在.cpp文件中添加以下代碼:

void FirstViewController::viewDidLoad()
{
    //獲取屏幕寬度
    DSize size = this->getView()->getBounds().size;
     
    //設(shè)置背景顏色為黑色
    this->getView()->setColor(CAColor_black);
     
    //創(chuàng)建Button
    CAButton* imageBtn = CAButton::createWithCenter(DRect(size.width*0.5, 500, 200, 50), CAButtonTypeSquareRect);
     
    //設(shè)置Buttion文本
    imageBtn->setTitleForState(CAControlStateAll, "Click");
     
    //設(shè)置tag值
    imageBtn->setTag(1);
     
    //設(shè)置按鈕監(jiān)聽
    imageBtn->addTarget(this, CAControl_selector(FirstViewController::respondTouch), CAControlEventTouchUpInSide);
     
    //添加到屏幕
    this->getView()->addSubview(imageBtn);
}
 
void FirstViewController::respondTouch(CAControl* btn, DPoint point)
{
    //獲得屏幕大小
    DSize size = this->getView()->getBounds().size;
     
    //創(chuàng)建CAAlerView 并設(shè)置顯示文本和 green按鈕和yellow按鈕
    CAAlertView* alertView = CAAlertView::createWithText("ButtonImage", UTF8("點(diǎn)擊替換按鈕顏色"), "green", "yellow", NULL);
     
    //獲得0-1之間的隨機(jī)數(shù)
    float randNum = CCRANDOM_0_1();
     
    if (randNum > 0.333f)
    {
        //添加按鈕設(shè)置文本為orange
        alertView->addButton("orange");
    }
    if (randNum> 0.666f)
    {
        //添加按鈕并設(shè)置文本為blue
        alertView->addButton("blue");
    }
     
    //顯示彈窗(如果不調(diào)用,彈窗不顯示)
    alertView->show();
     
    //設(shè)置彈窗按鈕的回調(diào)
    alertView->setTarget(this, CAAlertView_selector(FirstViewController::alertViewCallback));
}
 
void FirstViewController::alertViewCallback(int btnIndex)
{
    //根據(jù)tag獲得imageBtn對象
    CAButton* imageBtn =(CAButton*) this->getView()->getSubviewByTag(1);
     
    //根據(jù)CAAlertView上按鈕的index判斷響應(yīng)的邏輯
    if (btnIndex == 0)
    {                   
        //設(shè)置imageBtn背景色為green
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_green));
    }
    else if (btnIndex == 1)
    {
        //設(shè)置imageBtn背景色為yellow
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_yellow));
    }
    else if (btnIndex == 2)
    {
        //設(shè)置imageBtn背景色為orange
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_orange));
    }
    else
    {
        //設(shè)置imageBtn背景色為blue
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_blue));
    }
}

CAAlertView 方法說明

void setMessageFontName(std::string &var);

返回值:void

參數(shù):

類型參數(shù)名說明
std::stringvar信息文字

解釋:設(shè)置提示信息的字體


void setTitle(std::string var, CAColor4B col);

返回值:void

參數(shù):

類型參數(shù)名說明
CAColor4Bcol標(biāo)題顏色
std::stringvar信息文字

解釋:設(shè)置提示框的標(biāo)題


void setAlertMessage(std::string var, CAColor4B col);

返回值:void

參數(shù):

類型參數(shù)名說明
CAColor4Bcol提示信息顏色
std::stringvar提示信息字體

解釋:設(shè)置提示框的提示信息


void addButton(const std::string& btnText, CAColor4B col = ccc4(3, 100, 255, 255), CAImage* pNormalImage = NULL,AImage* pHighlightedImage = NULL);

返回值:void

參數(shù):

類型參數(shù)名說明
std::string&btnText按鈕文字
CAColor4Bcol按鈕顏色
CAImage*pNormalImage按鈕圖像
AImage*pHighlightedImage高亮度圖像

解釋:添加一個(gè)按鈕到CAAlertView


void setTarget(CAObject* target, SEL_CAAlertBtnEvent selector);

返回值:void

參數(shù):

類型參數(shù)名說明
CAObject*target監(jiān)聽目標(biāo)
SEL_CAAlertBtnEventselector監(jiān)聽選擇器

解釋:設(shè)置 CAAlertView 的監(jiān)聽


void show();

返回值:void

參數(shù):

解釋:設(shè)置顯示提示框


void hide();

返回值:void

參數(shù):

解釋:設(shè)置隱藏提示框void


static bool hideWithDisplayed();

返回值:static bool

參數(shù):

解釋:隱藏提示框


static CAAlertView* create();

返回值:static CAAlertView*

參數(shù):

解釋:創(chuàng)建


static CAAlertView* createWithText(const char* pszTitle, const char* pszAlertMsg, const char* pszBtnText, ...);

返回值:CAAlertView*

參數(shù):

類型參數(shù)名說明
const char*pszTitle標(biāo)題
const char*pszAlertMsg提示框內(nèi)容
const char*pszBtnText按鈕文本

解釋:創(chuàng)建,并指定其Text


bool initWithText(const char* szTitle, const char* szAlertMsg, const char* pszBtnText, ...);

返回值:bool

參數(shù):

類型參數(shù)名說明
const char*szTitle標(biāo)題
const char*szAlertMsg提示框內(nèi)容
const char*pszBtnText按鈕文本

解釋:初始化,并指定化文本


void addButton(CAButton* pBtn);

返回值:void

參數(shù):

類型參數(shù)名說明
CAButton*pBtn按鈕

解釋:添加按鈕到CAAlertView

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)