HuS Time is limited, To be a better man

Ios学习笔记——利用delegate实现两个页面间的数据传输

目标

从A页面通过按钮跳转到B,在B页面设置参数,然后返回A页面,并将参数传递回A页面

实现

实现两个页面间的数据传输有多种方法

  • 使用SharedApplication,定义一个变量来传递
  • 使用文件,或者NSUserdefault来传递
  • 通过一个单例的class来传递
  • 通过Delegate来传递 等

然后今天想在Demo里使用delegate完成传值,以为之前看过协议和委托,应该能轻松完成。但是却花了几个小时才完成,下面是含泪总结:

1. 在B页面.h文件声明协议,并设置公共变量

@protocol SettingViewPassValueDelegate <NSObject>
- (void)passSettingValue:(NSMutableArray )noiseSwitch noiseVolume:(NSMutableArray )noiseVolume;
- (void)passTestValue;
@end

//防止循环引用 @property (nonatomic, weak) id<SettingViewPassValueDelegate> delegate;

2. 在B页面需要的地方调用委托函数

这里在viewDidAppear时调用。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:(BOOL)animated];

[self.delegate passTestValue];
[self.delegate passSettingValue:self.nosieSwitch noiseVolume:self.nosieVolumeValue];

}

3. 在A页面.m文件接收协议,并实现必要委托函数

#import "B.h"

@interface MeditationViewController () <SettingViewPassValueDelegate>//接受协议 @end

pragma mark - delagate methods

  • (void)passSettingValue:(NSMutableArray )noiseSwitch noiseVolume:(NSMutableArray )noiseVolume { self.noiseSwitch = noiseSwitch; self.noiseVolumeValue = noiseVolume; NSLog(@"did i ?"); }

  • (void)passTestValue { NSLog(@"here is delagate"); }

4. 最重要的一步!指明委托对象!指明B的委托对象是A-ViewController

我就是在这一步出错的,因为网上的教程和博客比较老,而且举得例子基本上是抄来抄去,所以我一直没发现我的错误在哪里。 因为我实在storyboard上创建的viewController,并且A页面是通过一个按钮以Segue的方式跳转到B页面,所以我这样设置之后就正确了。

- (void)prepareForSegue:(nonnull UIStoryboardSegue )segue sender:(nullable id)sender
{
    if ([segue.identifier isEqualToString:@"toSettingView"]) {//注意设置segue的id
        SettingViewController svc = [[SettingViewController alloc] init];
        svc = segue.destinationViewController;
        svc.delegate = self;//就是在这里设置委托对象
    }
}

我之前想通过下面的代码设置委托对象,结果发现是错误的:

UIStoryboard storyboard = [UIStoryboard storyboardWithName:@"main" bundle:[NSBundle mainBundle]];
SettingViewController settingViewController = (SettingViewController *)[storyboard instantiateViewControllerWithIdentifier:@"SettingViewController"];  

NSLog了一下,发现Segue后出现的SettingViewController实例和上面代码获得的实例是不同的。原因暂时还没研究,先这样了。

Ios学习笔记——设置导航栏全透明效果

状态栏全透明效果

今天想在Demo里设置导航栏全透明效果,并与视图背景颜色相同。查找方法的过程中发现网上的一些方法不是很适用,然后看了下文档,试了一下,发现以下两句代码就可以实现效果。

文档说明:

The default value is nil, which corresponds to the default shadow image. When non-nil, this property represents a custom shadow image to show instead of the default. For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

代码:

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init]
    forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];

设置状态栏样式

顺便记录一下如何设置状态栏样式

1.设置项目Info中项目属性,添加View controller-based status bar appearance,设置为NO

2.设置代码

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

整体效果图如下:

Ios学习笔记——添加音效和播放音乐

给Button添加音效

#import <AudioToolbox/AudioToolbox.h>

@interface WebViewController () <UIWebViewDelegate> { SystemSoundID soundID;//实例变量 } @end

  • (IBAction)goForward:(id)sender { if (self.webView.canGoForward) { [self.webView goForward]; } //传入音效名称和后缀名 [self playSoundEffect:@"pop" type:@"wav"]; }

-(void)playSoundEffect:(NSString )name type:(NSString )type { //得到音效文件的地址 NSString soundFilePath =[[NSBundle mainBundle] pathForResource:name ofType:type]; //将地址字符串转换成url NSURL soundURL = [NSURL fileURLWithPath:soundFilePath];

//生成系统音效id
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &amp;soundID);
//播放系统音效
AudioServicesPlaySystemSound(soundID);

}

点击Button播放音乐

#import "ViewController.h"

import <AVFoundation/AVFoundation.h>//导入AVFoundation库

@interface ViewController () <AVAudioPlayerDelegate>//接受委托 @property AVAudioPlayer *player; @end

  • (IBAction)play:(id)sender { NSString soundFilePath = [[NSBundle mainBundle] pathForResource:@"life" ofType:@"mp3"]; if (soundFilePath) { NSURL soundURL = [NSURL fileURLWithPath:soundFilePath]; NSLog(@"%@",soundURL); NSError error = nil; AVAudioPlayer player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];

      self.player = player;
      self.player.volume = 1;
      self.player.numberOfLoops = 0;
      self.player.delegate = self;//设置委托
      [self.player prepareToPlay];
      if(error){
          NSLog(@"初始化播放器过程发生错误,错误信息:%@",error.localizedDescription);
      }
      [self.player play];
    

    } }

Ios学习笔记——添加手势操作

UIGestureRecognizer抽象类

UIGestureRecognizer抽象类不能直接使用,而是使用它的不同子类。

  • UITapGestureRecognizer: 点击
  • UISwipeGestureRecognizer: 滑动
  • UIPanGestureRecognizer: 滑动
  • UIPinchGestureRecognizer: 缩放
  • UIRotationGestureRecognizer: 转换方向
  • UILongPressGestureRecognizer: 长按操作
  • UIScreenEdgePanGestureRecognizer: 边缘滑动

如何在视图中添加手势操作

步骤一:创建并初始化UIGestureRecognizer的子类

//创建初始化一个UIGestureRecognizer的子类UITapGestureRecognizer,target是self,动作是handleSingleTapGesture:
UITapGestureRecognizer singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
//添加双击手势识别
UITapGestureRecognizer doubleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)];

//在识别手势前对单击手势暂不识别 [singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTapGestureRecognizer];

doubleTapGestureRecognizer.numberOfTapsRequired = 2; doubleTapGestureRecognizer.delaysTouchesBegan = YES;

根据需要设置UIGestureRecognizer的属性

步骤二:将手势依附到相应的视图上

[self.testView addGestureRecognizer:singleTapGestureRecognizer];
[self.testView addGestureRecognizer:doubleTapGestureRecognizer];

步骤三:实现手势事件

- (void)handleSingleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer
{
    CGFloat newWidth = 100.0;
    if (self.testView.frame.size.width == 100.0) {
        newWidth = 200.0;
    }

CGPoint currentCenter = self.testView.center;

self.testView.frame = CGRectMake(self.testView.frame.origin.x, self.testView.frame.origin.y, newWidth, self.testView.frame.size.height);
self.testView.center = currentCenter;

}

  • (void)handleDoubleTapGesture:(UITapGestureRecognizer *)tapGestureRecognizer { CGSize newSize = CGSizeMake(100.0, 100.0); if (self.testView.frame.size.width == 100.0) { newSize.width = 200.0; newSize.height = 200.0; }

    CGPoint currentCenter = self.testView.center;

    self.testView.frame = CGRectMake(self.testView.frame.origin.x, self.testView.frame.origin.y, newSize.width, newSize.height); self.testView.center = currentCenter; }

2014年我看过最好的10部电影

2014年,同往年一样,看了好多电影,作为一个电影迷,总归留个记录吧。 回忆了一下今年看得电影,挑出了以下10部认为值得一看的电影。