zoukankan      html  css  js  c++  java
  • 通过 objc_setAssociatedObject alert 和 button关联 及传值

    原文地址

    http://blog.csdn.net/lengshengren/article/details/16886915

     

    //唯一静态变量key

    static const char associatedkey;

    static const char associatedButtonkey;


    - (IBAction)sendAlert:(id)sender

    {

        

        NSString *message =@"我知道你是按钮了";

        

        UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"我要传值·" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];

        alert.delegate =self;

        [alert show];


         //#import <objc/runtime.h>头文件

         //objc_setAssociatedObject需要四个参数:源对象,关键字,关联的对象和一个关联策略。


         //1 源对象alert

        //2 关键字 唯一静态变量key associatedkey

        //3 关联的对象 sender

        //4 关键策略  OBJC_ASSOCIATION_RETAIN_NONATOMIC

        

        

        objc_setAssociatedObject(alert, &associatedkey, message,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

        

        objc_setAssociatedObject(alert, &associatedButtonkey, sender,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

        

    }

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

    {

        

        

        //通过 objc_getAssociatedObject获取关联对象

        NSString  *messageString =objc_getAssociatedObject(alertView, &associatedkey);

        

        

        UIButton *sender = objc_getAssociatedObject(alertView, &associatedButtonkey);

        

        _labebutton.text = [[sendertitleLabel]text];

        _ThisLabel.text = messageString;

        

        

        //使用函数objc_removeAssociatedObjects可以断开所有关联。通常情况下不建议使用这个函数,因为他会断开所有关联。只有在需要把对象恢复到“原始状态”的时候才会使用这个函数。

    }

    demo http://download.csdn.net/detail/lengshengren/6594365

     
  • 相关阅读:
    一些精简的小技巧
    POJ题目分类(转)
    【慢慢学算法】:连通图
    【菜鸟做水题】: 杭电1004
    杭电ACM试题分类,一步一个脚印!(转)
    【慢慢学Android】:获得当前时间
    【慢慢学算法】:小白鼠排队
    【慢慢学Android】:12.Notification示例
    【慢慢学Android】:13.打电话代码
    “/”应用程序中的服务器错误。
  • 原文地址:https://www.cnblogs.com/xiaobaizhu/p/3994327.html
Copyright © 2011-2022 走看看