How to change checkimages for single button? - ios4

I implemented the code to check button image. Now i want to change the images in that button when i click but whenever i click it displays only single image.
here i a implement like this
-
(IBAction)name:(id)sender{
currenttagvalue = [sender tag];int count = 0;
switch (currenttagvalue) {
case 1:
if (count == 0)
{
[level1 setImage:[UIImage imageNamed:#"one_time_selected.png"]
forState:UIControlStateNormal];
count++'
}
if (count == 1)
{
[level1 setImage:[UIImage imageNamed:#"two_time_selected.png"]
forState:UIControlStateNormal];
count++'
}
if (count == 2)
{
[level1 setImage:[UIImage imageNamed:#"one_time_selected.png"]
forState:UIControlStateNormal];
count++'
}
if (count == 2)
{
[level1 setImage:[UIImage imageNamed:#"three_time_selected.png"]
forState:UIControlStateNormal];
count++'
}
Thank you for in advance

Declare the count value as static, like:
static int count;

Related

How to add Custom Subview in keyboard view and on top view away? (work in iOS8 and below not work in iOS9)

My Step:
I Get keyboard view from code below (GetKeyboardView)
I Create custom view and then add to keyboard view form (1.)
I have question, Why it's work on iOS8 and bellow but not work in iOS9.
Keyboard view is on top every view in iOS9. How to solve this problem? please suggest me.
Code for Get the keyboard view (it's work)
-(UIView *)GetKeyboardView{
// locate keyboard view
int windowCount = (int)[[[UIApplication sharedApplication] windows] count];
if (windowCount < 2) {
return nil;
}
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView* keyboard;
for(int i = 0 ; i < [tempWindow.subviews count] ; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:#"<UIPeripheralHost"] == YES){
return keyboard;
}//This code will work on iOS 8.0
else if([[keyboard description] hasPrefix:#"<UIInputSetContainerView"] == YES){
for(int i = 0 ; i < [keyboard.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:#"<UIInputSetHost"] == YES){
return keyboard;
}
}
}
}
return nil;
}
Use
UIWindow* tempWindow = [UIApplication sharedApplication].windows.lastObject;
instead of
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];

collectionView, didSelectItemAtIndexPath some issue

i want to push next controller,but unsuccess.what should i do?
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
NSInteger section = indexPath.section, row = indexPath.row;
if (section == 1) {
if (row == 0) {
DetailsViewController * DetailsVC = [DetailsViewController alloc]init];
[self.navigationController pushViewController: DetailsVC animated:YES];
} else if (row == 1) {
}
} else if
(section == 2) {
}
}
you have to set identifier for viewcontroller and call the method like
CallViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:#"CallViewController"];
[self.navigationController pushViewController:vc animated:YES];
and be sure you had embedded Navigation View Controller.

Control Center, doesn't pause/play my audio stream

Hope that the issue goes to the correct stream.
I play HLS, i.e. I use AVPlayer for playing a mixed content( audio & pure video) from a server.
I keep playing audio after moving the app into background: I have enabled such feature in plist, plus I do store Prev Player, disable visual tracks, and set to nil AVPlayerLayer.
Here are the methods:
- (void)changePlayerState:(BOOL)restored
{
if (restored && !self.storedPlayer)
return;
if (!restored){
self.storedPlayer = self.playerView.player;
[self.playerView setPlayer:nil];
} else if (self.storedPlayer) {
[self.playerView setPlayer:self.storedPlayer];
self.storedPlayer = nil;
}
AVPlayerItem *playerItem = self.playerView.playerItem;
NSArray *tracks = [playerItem tracks];
for (AVPlayerItemTrack *playerItemTrack in tracks)
{
//
if ([playerItemTrack.assetTrack hasMediaCharacteristic:AVMediaCharacteristicVisual])
playerItemTrack.enabled = restored; //
}
}
And following one:
- (void)setPlayer:(AVPlayer *)player
{
if (_player != player) {
[self removePlayerObservers];
_player = player;
[self configurePlayerLayer];
if (_player)
[self addPlayerObservers];
}
}
- (void)removePlayerObservers
{
if ([_player observationInfo] != nil) {
[_player removeObserver:self
forKeyPath:#"rate"
context:NPUIPlayerViewPlayerRateObservationContext];
[_player removeObserver:self
forKeyPath:#"isExternalPlaybackActive"
context:NPUIPlayerViewAirPlayVideoActiveObservationContext];
}
[self removePeriodicTimeObserver];
}
- (void)addPlayerObservers {
[_player addObserver:self
forKeyPath:#"rate"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:NPUIPlayerViewPlayerRateObservationContext];
[_player addObserver:self
forKeyPath:#"isExternalPlaybackActive"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:NPUIPlayerViewAirPlayVideoActiveObservationContext];
[self addPeriodicTimeObserver];
}
- (void)removePeriodicTimeObserver
{
if (_registeredTimeObserver) {
[_player removeTimeObserver:_registeredTimeObserver];
_registeredTimeObserver = nil;
}
}
- (void)addPeriodicTimeObserver
{
__weak NPUIPlayerView *weakSelf = self;
_registeredTimeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.5f, 10) queue:dispatch_get_main_queue()
So after moving the app to the background , pressing HOme button, and activating Control Center(sliding up from the button, where is a calculator, camera, light buttons) , I can set the title of item being played, but apple's player controls doesn't work : pressing pause, play doesn't work also slider doesn't work.
I do receive events when I press the buttons, but at the same time, inspite of the fact, that I am calling [player pause], or change rate it doesn't change the button. But when there is an issue with accessing audio via internet, or sound buffer is empty, the button is changed, it has paused UI.
Below goes the code for setting now playing media center and adding commands
- (void)adjustNowPlayingScreen
{
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *mDic =[NSMutableDictionary dictionary];
NSString *newTitle = self.playerTitle.text ?: self.itemToPlay.shortName ?: self.itemToPlay.name ?: #"";
if (newTitle.length)
mDic[MPMediaItemPropertyTitle] = newTitle;
AVPlayerItem * item = [self.playerView.player currentItem];
CMTime itemDuration = [self.playerView.player.currentItem duration];
if (CMTIME_IS_VALID(itemDuration) ) {
NSTimeInterval duration = CMTimeGetSeconds(itemDuration);
if (duration)
mDic[MPMediaItemPropertyPlaybackDuration] = #(duration);
}
else
{
NSTimeInterval duration = CMTimeGetSeconds([item.asset duration]);
if (!isnan(duration) && duration > 0)
mDic[MPMediaItemPropertyPlaybackDuration] = #(duration);
else {
duration = CMTimeGetSeconds([[[[self playerView] playerItem] asset] duration]);
if (!isnan(duration) && duration > 0)
mDic[MPMediaItemPropertyPlaybackDuration] = #(duration);
}
}
NSString *urlStr = self.itemToPlay.autoQualityURL ?: self.itemToPlay.lowAutoQualityURL;
if (urlStr.length)
mDic[MPMediaItemPropertyAssetURL] = urlStr;
CMTime curTime = self.playerView.playerItem.currentTime;
if (CMTIME_IS_VALID(curTime)) {
NSTimeInterval duration = CMTimeGetSeconds(curTime);
mDic[MPNowPlayingInfoPropertyElapsedPlaybackTime]= #(duration);
}
if (mDic.count) {
#warning #"HACK: Necessary to change number of played items & index"
mDic[ MPMediaItemPropertyAlbumTrackNumber] = #(1);
mDic[ MPMediaItemPropertyAlbumTrackCount] = #(1);
mDic[ MPNowPlayingInfoPropertyPlaybackRate] = #(self.playerView.isPlaying ? 1.0 : 0.0);
UIImage *img = [UIImage imageNamed:#"team_4f6ae0b99d4b856118000124"];
mDic[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork alloc]initWithImage:img];
infoCenter.nowPlayingInfo = mDic;
}
}
- (void)addRemoteCommands
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
MPRemoteCommand *command = [commandCenter pauseCommand];
command.enabled = true;
[command addTarget:self action:#selector(pauseCommand:)];
command = [commandCenter playCommand];
command.enabled = true;
[command addTarget:self action:#selector(playCommand:)];
command = [commandCenter togglePlayPauseCommand];
command.enabled = true;
[command addTarget:self action:#selector(toggleCommand:)];
command = [commandCenter seekForwardCommand];
command.enabled = true;
[command addTarget:self action:#selector(seekForwardCommand:)];
command = [commandCenter seekBackwardCommand];
command.enabled = true;
[command addTarget:self action:#selector(seekBackwardCommand:)];
}
- (void)updateElapsedTime
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
MPRemoteCommand *command = [commandCenter togglePlayPauseCommand];
if (!command.enabled)
return;
[self adjustNowPlayingScreen];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
[self updateElapsedTime];
});
}
- (void)removeRemoteCommands
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
MPRemoteCommand *command = [commandCenter pauseCommand];
command.enabled = false;
[command removeTarget:self action:#selector(pauseCommand:)];
command = [commandCenter playCommand];
command.enabled = false;
[command removeTarget:self action:#selector(playCommand:)];
command = [commandCenter togglePlayPauseCommand];
command.enabled = false;
[command removeTarget:self action:#selector(toggleCommand:)];
command = [commandCenter seekForwardCommand];
command.enabled = false;
[command removeTarget:self action:#selector(seekForwardCommand:)];
command = [commandCenter seekBackwardCommand];
command.enabled = false;
[command removeTarget:self action:#selector(seekBackwardCommand:)];
}
- (void)seekBackwardCommand:(MPRemoteCommandEvent *)event
{
NSLog(#"%#",NSStringFromClass([event class]));
}
- (void)seekForwardCommand:(MPRemoteCommandEvent *)event
{
NSLog(#"%#",NSStringFromClass([event class]));
}
- (void)pauseCommand:(MPRemoteCommandEvent *)event
{
[self pause]; //_player pause];
AVPlayerItem *playerItem = self.playerView.playerItem;
NSArray *tracks = [playerItem tracks];
for (AVPlayerItemTrack *playerItemTrack in tracks)
{
/
if ([playerItemTrack.assetTrack hasMediaCharacteristic:AVMediaCharacteristicAudible])
playerItemTrack.enabled = false; /
}
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *mDic =[NSMutableDictionary dictionaryWithDictionary:infoCenter.nowPlayingInfo];
mDic[ MPNowPlayingInfoPropertyPlaybackRate] = #(0.0);
infoCenter.nowPlayingInfo = mDic;
}
So , my pause command is called, I call AVPlayer's pause method. But it doesn't stop audio stream.
And Slider doesn't work, therefore my seekBackwardCommand/seekForwardCommand are not called.
But as I said when the stream is over (audio also) the player on Control Center changes button from playing into pause, i.e. somehow it listen to Audio Session changes.
I adjust the sound category by setting AVAudioSessionCategoryPlayback, and setting mode : AVAudioSessionModeMoviePlayback
Please help how properly to handle pause/ play buttons on Control Screen, how to enable slider?
I am running my code on iOS9, iPhone 6.

how to get userintraction in uiwebview when added as sub view on a button in ios

I have a button performs some action, have added uiwebview as a sub view of the button to load a small gif animation, I can tap the button to perform the task but I needed to perform the same action when I tap the web view.
-(void)numeritsIphone4{
arrayButtonNames = #[#"Button1.png",#"Button2",#"Button3",#"Button4",#"Button5.png",#"Button6.png",#"Button7.png",#"Button8.png",#"Button9.png",#"Button10.png"];
float y = 20;
int x = 3;
int count = 0;
for (int i = 0 ; i < 10; i++)
{
count ++;
buttonsarray[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonsarray[i].frame = CGRectMake(x, y, 155, 90);
buttonsarray[i].tag = i;
[buttonsarray[i] setBackgroundImage:[UIImage imageNamed:[arrayButtonNames objectAtIndex:i]] forState:UIControlStateNormal];
[buttonsarray[i].titleLabel setFont:[UIFont fontWithName:#"Verdana" size:80]];
[buttonsarray[i] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//buttonsarray[i].titleLabel.textAlignment = UITextAlignmentLeft;
buttonsarray[i].contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[buttonsarray[i] setTitle:[NSString stringWithFormat:#"%d",digit] forState:UIControlStateNormal];
digit ++;
character = [[UIWebView alloc]initWithFrame:CGRectMake(100, 30, 25, 25)];
NSURL *url=[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle]pathForResource:#"Star" ofType:#"gif"]];
character.opaque = NO;
character.backgroundColor = [UIColor clearColor];
character.scrollView.scrollEnabled = NO;
[character loadRequest:[NSURLRequest requestWithURL:url]];
character.userInteractionEnabled = YES;
[buttonsarray[0] addSubview:character];
[character release];
[url release];
[buttonsarray[i] addTarget:self action:#selector(performOperation4:) forControlEvents:UIControlEventTouchUpInside];
x = x + 159;
[self.view addSubview:buttonsarray[i]];
if(count == 2)
{
count = 0;
x = 3;
y = y+ 92;
}
}
A possibility is to add a UITapGestureRecognizer to your UIWebView control and call the same function when the gesture recognizes.
NOTE: This way you have limitation if you have some buttons, links etc on your UIWebView page, that button, links might not get trigger. So give it a try and see if it helps you.

NSMutableArray removeAllObjects issue

I am trying to remove all the objects from the NSMutableArray and I am reloading the UITableView. But it produces SIGABRT in cellForRowAtIndexPath. The code is given below.
if ([nsMutableArray count] != 0) {
[nsMutableArray removeAllObjects];
[tableView reloadData];
}
It shows SIGABRT in
if (cell == nil) {
cell = (UICustomCell *)[nsMutableArray objectAtIndex:indexPath.row];
}
Help me to come out of this problem.
change this
to
if ([NSMutableArrayObject count] != 0) {
[NSMutableArrayObject removeAllObjects];
[tableView reloadData];
}
if (cell == nil) {
cell = (UICustomCell *)[NSMutableArrayObject objectAtIndex:indexPath.row];
}
You are calling instance methods (count, removeAllObjects, reloadData, objectAtIndex:) on classes (NSMutableArray, UITableView) instead of object instances.

Resources