CAAlertView(提示框)

类说明

CAAlertView是提示框控件,如果提示框内的按钮个数不超过三个,这个横向排列按钮,如果按钮个数超过三个则纵向排列。


CAAlertView 方法 (点击方法名可查看方法介绍)

方法说明
addButton添加一个按钮到CAAlertView
setAlertMessage提示框的提示信息 
hide隐藏提示框
setMessageFontName提示信息的字体 
show显示提示框
setTarget添加监听 
setTitle提示框的标题
hideWithDisplayed隐藏提示框
create创建
createWithText创建,并指定其Text
initWithText初始化,并指定化文本
addButton添加按钮到CAAlertView

CAAlertView是提示框控件,如果提示框内的按钮个数不超过三个,这个横向排列按钮,如果按钮个数超过三个则纵向排列。


我们来看一下实例代码:
首先在.h文件中声明一下函数:

//按钮的回调函数
    void respondTouch(CAControl* btn, DPoint point);
 
//提示框的回调函数
    void alertViewCallback(int btnIndex);

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

void FirstViewController::viewDidLoad()
{
    //获取屏幕宽度
    DSize size = this->getView()->getBounds().size;
     
    //设置背景颜色为黑色
    this->getView()->setColor(CAColor_black);
     
    //创建Button
    CAButton* imageBtn = CAButton::createWithCenter(DRect(size.width*0.5, 500, 200, 50), CAButtonTypeSquareRect);
     
    //设置Buttion文本
    imageBtn->setTitleForState(CAControlStateAll, "Click");
     
    //设置tag值
    imageBtn->setTag(1);
     
    //设置按钮监听
    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;
     
    //创建CAAlerView 并设置显示文本和 green按钮和yellow按钮
    CAAlertView* alertView = CAAlertView::createWithText("ButtonImage", UTF8("点击替换按钮颜色"), "green", "yellow", NULL);
     
    //获得0-1之间的随机数
    float randNum = CCRANDOM_0_1();
     
    if (randNum > 0.333f)
    {
        //添加按钮设置文本为orange
        alertView->addButton("orange");
    }
    if (randNum> 0.666f)
    {
        //添加按钮并设置文本为blue
        alertView->addButton("blue");
    }
     
    //显示弹窗(如果不调用,弹窗不显示)
    alertView->show();
     
    //设置弹窗按钮的回调
    alertView->setTarget(this, CAAlertView_selector(FirstViewController::alertViewCallback));
}
 
void FirstViewController::alertViewCallback(int btnIndex)
{
    //根据tag获得imageBtn对象
    CAButton* imageBtn =(CAButton*) this->getView()->getSubviewByTag(1);
     
    //根据CAAlertView上按钮的index判断响应的逻辑
    if (btnIndex == 0)
    {                   
        //设置imageBtn背景色为green
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_green));
    }
    else if (btnIndex == 1)
    {
        //设置imageBtn背景色为yellow
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_yellow));
    }
    else if (btnIndex == 2)
    {
        //设置imageBtn背景色为orange
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_orange));
    }
    else
    {
        //设置imageBtn背景色为blue
        imageBtn->setBackGroundViewForState(CAControlStateNormal, CAView::createWithColor(CAColor_blue));
    }
}

CAAlertView 方法说明

void setMessageFontName(std::string &var);

返回值:void

参数:

类型参数名说明
std::stringvar信息文字

解释:设置提示信息的字体

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

返回值:void

参数:

类型参数名说明
CAColor4Bcol标题颜色
std::stringvar信息文字

解释:设置提示框的标题

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

返回值:void

参数:

类型参数名说明
CAColor4Bcol提示信息颜色
std::stringvar提示信息字体

解释:设置提示框的提示信息

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

返回值:void

参数:

类型参数名说明
std::string&btnText按钮文字
CAColor4Bcol按钮颜色
CAImage*pNormalImage按钮图像
AImage*pHighlightedImage高亮度图像

解释:添加一个按钮到CAAlertView

void setTarget(CAObject* target, SEL_CAAlertBtnEvent selector);

返回值:void

参数:

类型参数名说明
CAObject*target监听目标
SEL_CAAlertBtnEventselector监听选择器

解释:设置 CAAlertView 的监听

void show();

返回值:void

参数:

解释:设置显示提示框


void hide();

返回值:void

参数:

解释:设置隐藏提示框void


static bool hideWithDisplayed();

返回值:static bool

参数:

解释:隐藏提示框


static CAAlertView* create();

返回值:static CAAlertView*

参数:

解释:创建

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

返回值:CAAlertView*

参数:

类型参数名说明
const char*pszTitle标题
const char*pszAlertMsg提示框内容
const char*pszBtnText按钮文本

解释:创建,并指定其Text

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

返回值:bool

参数:

类型参数名说明
const char*szTitle标题
const char*szAlertMsg提示框内容
const char*pszBtnText按钮文本

解释:初始化并指定化文本

void addButton(CAButton* pBtn);

返回值:void

参数:

类型参数名说明
CAButton*pBtn按钮

解释:添加按钮到CAAlertView

作者:唐伯虎点蚊香,如若转载,请注明出处:https://www.web176.com/crossapp/10209.html

(0)
打赏 支付宝 支付宝 微信 微信
唐伯虎点蚊香的头像唐伯虎点蚊香
上一篇 2023年2月21日
下一篇 2023年2月21日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注