2015年6月24日星期三

protocol 协议使用

protocl 协议跟java的接口差不多的概念


首先要定义
@protocol PainterSetupViewDelegate;
这是定义接口
@protocol PainterSetupViewDelegate<NSObject>

- (void)painterSetupViewController:(PainterSetupViewController *) 
        controller setColor:(UIColor *)color;
- (void)painterSetupViewController:(PainterSetupViewController *) 
        controller setWidth:(float)width;
@end


使用他的时候

.h里定义他
@interface ViewController : UIViewController<PainterSetupViewDelegate>
{
    
}
@end


.m里就得实例化他了

@implementation ViewController



- (void)viewDidLoad
{

    [super viewDidLoad];
}


-(IBAction)PenClick         //  Pen Type
{
    [(MainPainterView *)self.view setpCurType:PEN];
    
}
-(IBAction)LineClick       // Line Type
{
    [(MainPainterView *)self.view setpCurType:LINE];
}

-(IBAction)CircleClick     // Circle Type
{
    [(MainPainterView *)self.view setpCurType:CIRCLE];
    
}

-(IBAction)EraseClick     // Erase Type
{
    [(MainPainterView *)self.view setpCurType:ERASE];
    
}

-(IBAction)RectangleClick     // Taegeuk Type
{
    [(MainPainterView *)self.view setpCurType:RECTANGLE];
    
}



-(IBAction)SetupClick
{
    if (pPainterSetupViewController == nil) {
            
        PainterSetupViewController *viewController = [self.storyboard     instantiateViewControllerWithIdentifier:@"PainterSetupViewController"];
        
        viewController.delegate = self;
        pPainterSetupViewController = viewController;
    }
    [self presentViewController:pPainterSetupViewController animated:YES completion:nil];
    
}


- (void)painterSetupViewController:(PainterSetupViewController *) controller setColor:(UIColor *)color
{
    [(MainPainterView *) self.view setpCurColor:color];
}

- (void)painterSetupViewController:(PainterSetupViewController *) controller setWidth:(float)width
{
    [(MainPainterView *) self.view setpCurWidth:width];
}



@end

2015年6月2日星期二

获取绑定的页面类(instantiateViewControllerWithIdentifier)


setupViewControll=[self.storyboard instantiateViewControllerWithIdentifier:@"SetupViewControllView"];

这个是绑定页面的. 当对应页面绑定对应类时 使用这个就可以直接拿过来 页面的绑定类了

之后可以直接用该页面的控件

比如
UIViewController A

UIViewController B{
  @property(weak,nonatomic)IBOutlet UISwitch *swichControl;
}

在A页面里 
有 B b=[self.storyboard instantiateViewControllerWithIdentifier:@"B"];
那么 b.swichControll就是页面B的 UISwitch控件了

使用Http 上传文件出错解决(java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE)

在 app gradle文件里 添加
compile('org.apache.httpcomponents:httpmime:4.3.6') {
        exclude module: 'httpclient'
    }
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
即可



//下面是上传文件. 可复制
public MultiPartRequest.ResponseResult httpUploadStringResult(String url,List<NameValuePair> params, List<NameValuePair> filePaths) throws Exception {

        String mCharSet = null;
        String mMimeType = null;
        MultiPartRequest multiPartRequest=new MultiPartRequest();
        MultiPartRequest.ResponseResult<String> responseResult=multiPartRequest.new ResponseResult();
        // 定义HttpClient对象
                HttpClient client = mHttpClient;

        HttpPost post = new HttpPost(HttpLoader.getServerUrl(url));
// post.addHeader("charset", HTTP.UTF_8);
        post.addHeader(
                "User-Agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; WOW64; SV1; .NET CLR 2.0.50727)");
        post.addHeader("Accept",
                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");

//        MultipartEntity me = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        MultipartEntityBuilder me=MultipartEntityBuilder.create();
        if (params!=null){
            for (int i = 0; i < params.size(); i++) {
                me.addPart(params.get(i).getName(), new StringBody(params
                        .get(i).getValue(), Charset.forName(HTTP.UTF_8)));
            }
        }

        if(filePaths!=null){
            if (filePaths.size()>0) {
                String[] userimg=new String[filePaths.size()];
                for (int i = 0; i < filePaths.size(); i++) {
                    userimg[i]=filePaths.get(i).getName();
                }
                for (int j = 0; j < filePaths.size(); j++) {
                    File tmpFile = new File(filePaths.get(j).getValue());
                    if (tmpFile != null && tmpFile.exists())
// me.addPart(filePaths.get(j).getName(), new InputStreamBody(
// new FileInputStream(tmpFile),
// "application/octet-stream", tmpFile.getName()));
                        me.addPart(userimg[j], new FileBody(tmpFile));
                }
            }
        }
        post.setEntity(me.build());
//        post.setEntity(me);
        HttpResponse resp = null;

        resp = client.execute(post);

        if (resp.getStatusLine().getStatusCode() == 200) {
            HttpEntity tmpHttpEntity = resp.getEntity();
            mCharSet = EntityUtils.getContentCharSet(tmpHttpEntity);
            Header tmpContentType = tmpHttpEntity.getContentType();
            mMimeType = null;
            if (tmpContentType != null) {
                HeaderElement[] tmpContentTypes = tmpContentType
                        .getElements();
                for (int i = 0; i < tmpContentTypes.length; i++) {
                    mMimeType = tmpContentTypes[i].getName();
                }
            }
            if (mCharSet == null)
                responseResult.resultDate=EntityUtils.toString(resp.getEntity());
            else
                responseResult.resultDate=EntityUtils.toString(resp.getEntity(), mCharSet);
        }
        responseResult.statusCode=resp.getStatusLine().getStatusCode();

        return responseResult;
    }

2015年6月1日星期一

国际化语言的使用 NSLocalizedString

1.选择国际化语言

2.添加文件。注:文件名必须是Localizable.strings