UIViewController called from Storyboard is nil - ios

I am calling a view with method;
[self.storyboard instantiateViewControllerWithIdentifier:];
Yesterday, this method was working well, but after some time something happened, it has stopped working.
I am using both xCode6 beta 6 and xcode5 v5.1.1
My code is as below, here all objects of an ViewController is nil?
What is wrong with this?
(IBAction)addTemplateViewPressed:(id)sender
{
PhotoTemplateViewController *templateVC = [self.storyboard instantiateViewControllerWithIdentifier:#"photoTemplateViewController"];
//NSLog(#"%#",NSStringFromCGRect(templateVC.view.frame));
if(photoVerticalArray.count !=0 )
{
photoVerticalArray = [templateVC setVerticalImages:photoVerticalArray];
}
else
{
verticalFinished = true;
}
if(photoHorizontalArray.count !=0)
{
photoHorizontalArray = [templateVC setHorizontalImages:photoHorizontalArray];
}
else
{
horizontalFinished = true;
}
if(!verticalFinished && !horizontalFinished)
{
CGRect temp = templateVC.mainView.frame;
if(lastPosition == 0)
{
temp.origin.y = lastPosition = templateVC.mainView.frame.origin.y+48;
}
else
{
lastPosition = temp.origin.y= lastPosition + temp.size.height-4;
}
templateVC.view.frame = temp;
[mainView addSubview:templateVC.view];
NSLog(#"%#",NSStringFromCGSize(mainScrollView.contentSize));
[mainScrollView setContentSize:CGSizeMake(0, temp.origin.y+temp.size.height+50)];
NSLog(#"%#",NSStringFromCGSize(mainScrollView.contentSize));
}

Related

UISlider is flickering

we are using UISlider in our project, we are using that older in order to play audio.for specific audio, when we are playing that, UISlider is flickering for first 13 seconds. for another audio, that UISlider is flickering for first 8 seconds. what can be the solution for this? Thanks in advance.
I have assigned right value to the slider.
- (void)setSliderValue
{
if(self.jwAudioPlayer == nil)
return;
if(self.slider && self.slider.maximumValue == 1.0)
{
self.slider.minimumValue = 0.0;
self.slider.maximumValue = self.jwAudioPlayer.duration;
self.slider.value = 0;
}
}
- (void)setPlayerCurrentPlayingTime
{
if(self.jwAudioPlayer == nil)
return;
if(self.slider.maximumValue <= 0 && self.jwAudioPlayer.duration > 0){
self.slider.maximumValue = self.jwAudioPlayer.duration;
}
CGFloat durationInSeconds = self.jwAudioPlayer.duration;
CGFloat currentTimeInSeconds = self.jwAudioPlayer.position;
if(currentTimeInSeconds >= durationInSeconds)
{
currentTimeInSeconds = durationInSeconds;
[self pause];
}
if(self.durationTime)
{
self.durationTime.text = [self timeFormatted:durationInSeconds];
}
if(self.currentPlayingTime)
{
self.currentPlayingTime.text = [self timeFormatted:currentTimeInSeconds];
}
if(self.slider && self.slider.tag == 0)
{
[self setSliderValue];
[self.slider setValue:currentTimeInSeconds animated:YES];
}
if(self.playerController)
[self.playerController enableDisableSkipImage:currentTimeInSeconds];

how does overriding a superclass method in a subclass cause the overridden method to be called by an object of type of super class

An example is the assignment 3 from stanford's cs193p:
-(int)match:(NSArray *)otherCards
{
int score = 0;
if ([otherCards count] == 1)
{
playingCard *otherCard = [otherCards firstObject];
if ([self.suit isEqualToString: otherCard.suit])
{
score = 1;
NSLog(#"%d",score);
}else if (self.rank == otherCard.rank)
{
score = 4;
}
}
return score;
}
The above is the implementation of a method in a subclass of Cards called PlayingCard.
- (int)match:(NSArray *)otherCards
{
int score = 0;
for (Card *cards in otherCards)
if ([cards.contents isEqualToString:self.contents])
score = 1;
return score;
}
The above is the implementation of match in Cards.
-(void)chooseCardAtIndex:(NSUInteger)index
{
Card *card = [self cardAtIndex:index];
if (!card.isMatched)
{
if (card.isChosen)
{
card.chosen = NO;
}
else
{
for (Card *otherCard in self.cards)
{
if (otherCard.isChosen && !otherCard.isMatched)
{
int matchScore = [card match:#[otherCard]];
if (matchScore)
{
self.score += matchScore * MATCH_BONUS;
card.matched = YES;
otherCard.matched = YES;
}
else
{
otherCard.chosen = NO;
self.score -= MISMATCH_PENALTY;
}
break;
}
}
self.score -= COST_TO_CHOOSE;
card.chosen = YES;
}
}
}
As you can see above, the method match is called by an instance of Card and not playingCard, yet the results follow implementation from playingCard
I think you need a virtual base class ,in superClass cards
-(int)match:(NSArray *)otherCards
{
// do nothing
}
and you need two subclass such as PlayingCard ,NormalCard .YOU Should implementation the function in each subClass.

shouldAutorotateToInterfaceOrientation method not called in ipad

I am trying to get the orientation of device when the device is rorate. my code is works good for iphone but for ipad shouldAutorotateToInterfaceOrientation method is not called. i have taken tab view controller as a root view controller and below code i have written on first tab's view controller.
Source Code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//[self configureForOrientation:interfaceOrientation];
return YES;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self configureForOrientation:toInterfaceOrientation];
}
-(void)configureForOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
NSLog(#"Land");
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
NSLog(#"Land ipad");
view_home.frame = CGRectMake(0,0,1004,768);
scroll_home.frame = CGRectMake(0,0,1004,768);
scroll_home.contentSize = CGSizeMake(scroll_home.frame.size.width,scroll_home.frame.size.height);
scroll_home.scrollEnabled = true;
btntitle.frame = CGRectMake(0,0,1004,52);
}
else
{
view_home.frame = CGRectMake(0,0,568,320);
status.frame = CGRectMake(0, 0, view_home.bounds.size.width, 20.0f);
scroll_home.frame = CGRectMake(0, 0, 568,430);
scroll_home.contentSize = CGSizeMake(scroll_home.frame.size.width,scroll_home.frame.size.height);
scroll_home.scrollEnabled = true;
btntitle.frame = CGRectMake(0,0,480,48);
}
}
else
{
NSLog(#"Port");
if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
view_home.frame = CGRectMake(0,0,768,1004);
scroll_home.frame = CGRectMake(0,0,768,1004);
//scroll_home.contentSize = CGSizeMake(scroll_home.frame.size.width,scroll_home.frame.size.height);
scroll_home.scrollEnabled = false;
btntitle.frame = CGRectMake(0,0,768,52);
}
else
{
view_home.frame = CGRectMake(0,0,320,548); //548
scroll_home.frame = CGRectMake(0, 0, 480, 568);
//scroll_home.contentSize = CGSizeMake(scroll_home.frame.size.width,scroll_home.frame.size.height);
scroll_home.scrollEnabled = false;
btntitle.frame = CGRectMake(0,0,320,48);
}
}
}

transform image when it hits the edge of the screen in iOS

I have 10 fireflies that I make "fly" around the screen using the code below.
The code also serves to keep the fireflies on the screen.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *blueArray = [NSArray array];
blueArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"blue1.png"],
[UIImage imageNamed:#"blue2.png"], nil];
blue.animationImages = blueArray;
blue.animationDuration = 0.20;
blue.animationRepeatCount = -1;
[blue startAnimating];
bluepos =CGPointMake(2.0, 1.5);
}
-(void) someMethod {
endingAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:#selector(makeFly) userInfo:nil repeats:YES];
}
-(void) makeFly {
blue.center = CGPointMake(blue.center.x+bluepos.x, blue.center.y+bluepos.y);{
if(blue.center.x > 480 || blue.center.x <0)
bluepos.x = -bluepos.x;
if(blue.center.y > 320 || blue.center.y <0)
bluepos.y = -bluepos.y;
}
}
The "flying" works great except that when the fireflies hit the edge of the screen and reverse direction to keep them on the screen the firefly image itself is still "facing" the othe direction so it looks like they are flying backwards half the time.
I want to set it up so that when the fireflies hit the edge of the screen they reverse direction AND the image itself is reversed.
I tried this:
In .h
#property (nonatomic, assign) BOOL blueIsFacingRight;
In .m
#synthesize blueIsFacingRight;
-(void) makeFly {
blue.center = CGPointMake(blue.center.x+bluepos.x, blue.center.y+bluepos.y); {
if(blue.center.x > 480 ) {
if (blueIsFacingRight == YES) {
blue.transform = CGAffineTransformMakeScale(-1, 1);
blueIsFacingRight = NO;
}
bluepos.x = -bluepos.x;
}
if(blue.center.x <0) {
bluepos.x = -bluepos.x;
if (blueIsFacingRight == NO) {
blue.transform = CGAffineTransformMakeScale(1, 1);
blueIsFacingRight = YES;
}
}
if(blue.center.y > 320 )
bluepos.y = -bluepos.y;
if( blue.center.y <0)
bluepos.y = -bluepos.y;
}
}
I thought this would work but the image does not reverse when it hits the "wall"
Can anyone explain why this does not work and if there is a better why to accomplish the effect im looking for?
You are setting the transform X value to -1 in both cases. When its facing right you should set transform like this CGAffineTransformMakeScale(1, 1);
blue.center = CGPointMake(blue.center.x+bluepos.x, blue.center.y+bluepos.y); {
if(blue.center.x > 480 ) {
if (blueIsFacingRight == YES) {
blue.transform = CGAffineTransformMakeScale(-1, 1);
blueIsFacingRight = NO;
}
bluepos.x = -bluepos.x;
}
if(blue.center.x <0) {
bluepos.x = -bluepos.x;
if (blueIsFacingRight == NO) {
blue.transform = CGAffineTransformMakeScale(1, 1);
blueIsFacingRight = YES;
}
}
**blue.transform =CGAffineTransformIdentity;**
THe above was in my makeFly method with a bunch of other stuff above where I was reversing the image so I was undoing the change the very next time the method was called ( 0.03 later)
Man I feel stupid

Reusing 3 views on UIScrollView's Paging

I did the following code where three views can be reused during Pagination in UIScrollView in order to save live memory-->
#pragma mark - UIScrollView Delegates
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat pageWidth = self.view.frame.size.width;
CGPoint aContentOffSet = [[self scrollView] contentOffset] ;
float currPos = aContentOffSet.x;
int selectedPage = roundf(currPos/pageWidth);
[[self pageControl] setCurrentPage:selectedPage];
[self update:selectedPage];
}
#pragma mark - Custom methods
-(void)update:(int) selectedPage{
BOOL view1FrameShallBeUnchanged = false;
BOOL view2FrameShallBeUnchanged = false;
BOOL view3FrameShallBeUnchanged = false;
BOOL aFrame1Matched = false;
BOOL aFrame2Matched = false;
BOOL aFrame3Matched = false;
CGRect aFrame1 = CGRectMake(selectedPage*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);
CGRect aFrame2 = CGRectMake((selectedPage-1)*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);
CGRect aFrame3 = CGRectMake((selectedPage+1)*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);
ViewOnScrollView *aView1 = (ViewOnScrollView*)[[self scrollView] viewWithTag:1234];
ViewOnScrollView *aView2 = (ViewOnScrollView*)[[self scrollView] viewWithTag:12345];
ViewOnScrollView *aView3 = (ViewOnScrollView*)[[self scrollView] viewWithTag:123456];
if(aView1 && aView2 && aView3){
//Check for Frame 1
if(aFrame1.origin.x == aView1.frame.origin.x){
view1FrameShallBeUnchanged = true;
aFrame1Matched = true;
}
else if(aFrame1.origin.x == aView2.frame.origin.x){
view2FrameShallBeUnchanged = true;
aFrame1Matched = true;
}
else if(aFrame1.origin.x ==aView3.frame.origin.x){
view3FrameShallBeUnchanged = true;
aFrame1Matched = true;
}
//Check for Frame 2
if(aFrame2.origin.x == aView1.frame.origin.x){
view1FrameShallBeUnchanged = true;
aFrame2Matched = true;
}
else if(aFrame2.origin.x == aView2.frame.origin.x){
view2FrameShallBeUnchanged = true;
aFrame2Matched = true;
}
else if(aFrame2.origin.x == aView3.frame.origin.x){
view3FrameShallBeUnchanged = true;
aFrame2Matched = true;
}
//Check for Frame 3
if(aFrame3.origin.x == aView1.frame.origin.x){
view1FrameShallBeUnchanged = true;
aFrame3Matched = true;
}
else if(aFrame3.origin.x == aView2.frame.origin.x){
view2FrameShallBeUnchanged = true;
aFrame3Matched = true;
}
else if(aFrame3.origin.x == aView3.frame.origin.x){
view3FrameShallBeUnchanged = true;
aFrame3Matched = true;
}
if(!view1FrameShallBeUnchanged){
if(!aFrame1Matched){
[aView1 setFrame:aFrame1];
}
else if(!aFrame2Matched){
[aView1 setFrame:aFrame2];
}
else{
[aView1 setFrame:aFrame3];
}
[self hideOrShowTheTabs:aView1];
[self hideShowView:aView1];
}
if(!view2FrameShallBeUnchanged){
if(!aFrame1Matched){
[aView2 setFrame:aFrame1];
}
else if(!aFrame2Matched){
[aView2 setFrame:aFrame2];
}
else{
[aView2 setFrame:aFrame3];
}
[self hideShowView:aView2];
}
if(!view3FrameShallBeUnchanged){
if(!aFrame1Matched){
[aView3 setFrame:aFrame1];
}
else if(!aFrame2Matched){
[aView3 setFrame:aFrame2];
}
else{
[aView3 setFrame:aFrame3];
}
[self hideShowView:aView3];
}
}
}
-(void)hideShowView:(ViewOnScrollView*)theView{
if(theView.frame.origin.x<0 || theView.frame.origin.x>[self.scrollView contentSize].width )
theView.hidden = YES;
else{
theView.hidden = NO;
}
}
Comments/Suggestions/Better ways to do the same are welcome..
check out this class ..maybe it can help.. easy to use...just like UITableview
VSScroller
Its ok, but you have too much code (~40 lines) and too much unnecessary processing. You only need to know when one frame matches (let's say the center frame), and also you should do this only when the page is about to change, not on every scroll event.
This way, whenever the left or right page becomes the current page, you move the opposite page to the other side.
Another bug you had is that you should hide the last+1 page when its frame.origin.x is equal (== , or >=) to the content size, not only bigger (>).
#pragma mark - UIScrollView Delegates
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
int selectedPage = roundf(newsPagesView.contentOffset.x/_pageWidth);
if (selectedPage != _currentPage) {
_currentPage = selectedPage;
[self update:selectedPage];
}
}
#pragma mark - Custom methods
-(void)update:(int) selectedPage{
BOOL page1FrameMatched = false;
BOOL page2FrameMatched = false;
BOOL page3FrameMatched = false;
BOOL frameCurrentMatched = false;
CGRect frameCurrent = CGRectMake(selectedPage*_pageWidth, 0.0f, _pageWidth, _pageHeight);
CGRect frameLeft = CGRectMake((selectedPage-1)*_pageWidth, 0.0f, _pageWidth, _pageHeight);
CGRect frameRight = CGRectMake((selectedPage+1)*_pageWidth, 0.0f, _pageWidth, _pageHeight);
NewsPage *page1 = (NewsPage*)[newsPagesView viewWithTag:100];
NewsPage *page2 = (NewsPage*)[newsPagesView viewWithTag:101];
NewsPage *page3 = (NewsPage*)[newsPagesView viewWithTag:102];
if(page1 && page2 && page3){
//Check for Current
if(frameCurrent.origin.x == page1.frame.origin.x){
page1FrameMatched = true;
frameCurrentMatched = true;
}
else if(frameCurrent.origin.x == page2.frame.origin.x){
page2FrameMatched = true;
frameCurrentMatched = true;
}
else if(frameCurrent.origin.x ==page3.frame.origin.x){
page3FrameMatched = true;
frameCurrentMatched = true;
}
if(frameCurrentMatched){
if(page1FrameMatched){
[page1 setFrame:frameCurrent];
[page2 setFrame:frameLeft];
[page3 setFrame:frameRight];
}
else if(page2FrameMatched){
[page1 setFrame:frameRight];
[page2 setFrame:frameCurrent];
[page3 setFrame:frameLeft];
}
else{
[page1 setFrame:frameLeft];
[page2 setFrame:frameRight];
[page3 setFrame:frameCurrent];
}
[self hideShowView:page1];
[self hideShowView:page2];
[self hideShowView:page3];
}
}
}
/**
* This method hides the view if it is outside the scrollview content bounds, i.e. the
* view before page 0, or the view after last page.
*/
-(void)hideShowView:(NewsPage*)aPage{
if(aPage.frame.origin.x<0 || aPage.frame.origin.x>=[newsPagesView contentSize].width )
aPage.hidden = YES;
else{
aPage.hidden = NO;
}
}
Checkout my example, branch:new
https://github.com/iSevenDays/RecyclingScrollView

Resources