Coder Social home page Coder Social logo

uialertview-by-value's Introduction

UIAlertView-By-Value

UIAlertView使用Runtime机制实现传值

百度经验:http://jingyan.baidu.com/article/48b558e32487bf7f38c09ab5.html

##代码

#import "ViewController.h"

// 1、包含头文件:
#import <objc/runtime.h>
// 2、定义静态唯一key
static char alertInfoKey;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 3、初始化
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"标题"
                                                    message:@"内容消息"
                                                   delegate:self
                                          cancelButtonTitle:@"取消"
                                          otherButtonTitles:@"确定", nil];
    
    // 4、显示
    [alert show];
    
    
    // 5、定义要传递的值
    NSString *passByValue = @"这是一个字符串";
    objc_setAssociatedObject(
                             alert,                     //对象
                             &alertInfoKey,             //键的引用
                             passByValue,               //传递的值
                             OBJC_ASSOCIATION_ASSIGN    //策略assign
                             );
}

#pragma mark - UIAlertViewDelegate
/**
 *  6、实现UIAlertViewDelegate方法
 *
 *  @param alertView   UIAlertView对象
 *  @param buttonIndex 选中下标
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == alertView.cancelButtonIndex) {
        NSLog(@"点击取消");
    }else{
        // 6、取值
        NSString *getValue = objc_getAssociatedObject(
                                                      alertView,        //对象
                                                      &alertInfoKey     //键的引用
                                                      );
        NSLog(@"取值:%@",getValue);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

uialertview-by-value's People

Contributors

carrysniper avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.