W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
類說明
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));
}
}
void setMessageFontName(std::string &var);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
std::string | var | 信息文字 |
解釋:設(shè)置提示信息的字體
void setTitle(std::string var, CAColor4B col);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
CAColor4B | col | 標(biāo)題顏色 |
std::string | var | 信息文字 |
解釋:設(shè)置提示框的標(biāo)題
void setAlertMessage(std::string var, CAColor4B col);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
CAColor4B | col | 提示信息顏色 |
std::string | var | 提示信息字體 |
解釋:設(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 | 按鈕文字 |
CAColor4B | col | 按鈕顏色 |
CAImage* | pNormalImage | 按鈕圖像 |
AImage* | pHighlightedImage | 高亮度圖像 |
解釋:添加一個(gè)按鈕到CAAlertView
void setTarget(CAObject* target, SEL_CAAlertBtnEvent selector);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
CAObject* | target | 監(jiān)聽目標(biāo) |
SEL_CAAlertBtnEvent | selector | 監(jiān)聽選擇器 |
解釋:設(shè)置 CAAlertView 的監(jiān)聽
返回值:void
參數(shù):
解釋:設(shè)置顯示提示框
返回值:void
參數(shù):
解釋:設(shè)置隱藏提示框void
static bool hideWithDisplayed();
返回值:static bool
參數(shù):
解釋:隱藏提示框
返回值: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
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: