Passing URL into method - iphone

Hi i have a question here,
i'm able to get the UrL string store in "imageName" variable successfully in showImage method.
I need to do the same in this method "segmentedControlIndexChanged" but i cant get the variable "imageName".
Is there anyway i can declare the IBAction method to work similarly as the showImage method?
I Tried ->" -(IBAction) segmentedControlIndexChanged:(NSString *)imageName{} " but it cant work, i'm unable to get the variable "imageName".
- (void) showImage:(NSString *)imageName
{
NSData *imageData;
//NSString * string1 = [NSString stringWithFormat:imageName];
imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageName]];
//NSLog(#"%#",imageName);
image = [[UIImage alloc] initWithData:imageData];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
if(image.size.height+20 >= 224)
{
[scrollview setContentSize:CGSizeMake(289, image.size.height + 20)];
}
else {
[scrollview setContentSize:CGSizeMake(289, 224)];
}
}
////////
-(IBAction) segmentedControlIndexChanged{
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
image = [UIImage imageNamed:image1name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
if(image.size.height+20 >= 224)
{
[scrollview setContentSize:CGSizeMake(289, image.size.height + 20)];
}
else {
[scrollview setContentSize:CGSizeMake(289, 224)];
}
break;
case 1:
image = [UIImage imageNamed:image2name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height)];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(289, image.size.height+20)];
break;
case 2:
image = [UIImage imageNamed:image3name];
self.imageView.image = image;
[imageView setFrame:CGRectMake(5, 10, image.size.width, image.size.height+1)];
[scrollview setScrollEnabled:YES];
[scrollview setContentSize:CGSizeMake(289, image.size.height+20)];
break;
default:
break;
}
}
EDIT
I have encountered Bad Access error, anyone knows what does it mean or can anyone show me some ways to solve it?

No, an IBAction can only have one of the following three signatures in iOS:
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender forEvent:(UIEvent *)event
EDIT
You could extend UIButton to store the value you need, like:
#interface MyButton : UIButton {
}
#property (nonatomic, retain) NSString *url;
#end
...
#implementation MyButton
#synthesize url;
#end
...
- (void)buttonTapped:(id)sender {
MyButton *button = (MyButton *)sender;
UIImage *image = [UIImage imageNamed:button.url];
...
}
EDIT
You can use tags to differentiate between different senders, like
enum {
ButtonOneTag = 128,
ButtonTwoTag,
...
}
...
- (void)buttonTapped:(id)sender {
UIButton *button = (UIButton *)sender;
switch(button.tag) {
case ButtonOneTag:
NSLog(#"Button One Tapped");
break;
case ButtonTwoTag:
NSLog(#"Button Two Tapped");
break;
}
You can also just compare the sender to ivars for the buttons like so:
- (void)buttonTapped:(id)sender {
//assuming buttonOne and buttonTwo are IBOutlets on this class
if (sender == self.buttonOne) {
NSLog(#"Button One Tapped");
else if (sender == self.buttonTwo) {
NSLog(#"Button Two Tapped");
}
...
}

Related

How to pass array of images to icarousel ? How to reload image array data to reload icarousel?

I am new to iOS . I want to pass different image arrays in different button actions . I want to reload iCarousel in every button action
Here is my code
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",index]]];
view.tag=index;
view.frame = CGRectMake(70, 80, 180, 260);
return view;
}
Presently i am passing images to icarousel like this.
Thanks in advance..
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [self.aryImages count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIView *viewForImages = (UIButton *)view;
if (viewForImages == nil)
{
UIImage *image = [UIImage imageNamed:[self.aryImages objectAtIndex:index]];
viewForImages = [[UIView alloc]init];
viewForImages.frame = imagesFrameLocal;
[viewForImages setBackgroundColor:[UIColor clearColor]];
UIView *viewCreation = [[UIView alloc]initWithFrame:viewForImages.frame];
[viewCreation setBackgroundColor:[UIColor whiteColor]];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(viewForImages.bounds.origin.x+5, viewForImages.bounds.origin.y+5, viewForImages.bounds.size.width-10, viewForImages.bounds.size.height-10)];
[btn setTag:index];
if(onlineImages == YES)
{
[btn setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[self.aryImages objectAtIndex:index]]] placeholderImage:[UIImage imageNamed:#"1.png"]];
}
else
{
[btn setBackgroundImage:image forState:UIControlStateNormal];
}
[btn addTarget:self.recievedDelegate action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[viewCreation addSubview:btn];
switch (index%5)
{
case 0:
[viewCreation setTransform:CGAffineTransformMakeRotation(3)];
break;
case 1:
[viewCreation setTransform:CGAffineTransformMakeRotation(19)];
break;
case 2:
[viewCreation setTransform:CGAffineTransformMakeRotation(3)];
break;
case 3:
[viewCreation setTransform:CGAffineTransformMakeRotation(19)];
break;
case 4:
[viewCreation setTransform:CGAffineTransformMakeRotation(3)];
break;
default:
[viewCreation setTransform:CGAffineTransformMakeRotation(19)];
break;
}
[viewCreation.layer setCornerRadius:15.0f];
[viewForImages addSubview:viewCreation];
}
return viewForImages;
}
Output::

UIButton ImageView Taking More Space in iCarousel and not Responding to touch Event

I am beginner trying to make a menu using iCarousel. What I have tried so far is return a UIButton with a custom image for each view in carousel. The image list has been declared same as told in examples. Problems I am facing are:
button (view returned in carousel) - its image is not adjusting properly as shown in fig.
Also you can see that when I am displaying bear, it is picking description of Tiger!
When I tap any pic, it just scroll forward instead of reacting to button touch down event.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//set up carousel data
wrap = YES;
self.menuItems = [NSMutableArray arrayWithObjects:#"Bear.png",
#"Zebra.png",
#"Tiger.png",
#"Goat.png",
#"Birds.png",
#"Giraffe.png",
nil];
self.descriptions = [NSMutableArray arrayWithObjects:#"Bears Eat: Honey",
#"Zebras Eat: Grass",
#"Tigers Eat: Meat",
#"Goats Eat: Weeds",
#"Birds Eat: Seeds",
#"Giraffes Eat: Trees",
nil];
}
return self;
}
- (void)buttonTapped:(UIButton *)sender
{
NSInteger index = [aCarousel indexOfItemViewOrSubview:sender];
switch (index) {
case 0:
NSLog(#"at index: %d",index);
break;
case 1:
NSLog(#"at index: %d",index);
break;
case 2:
NSLog(#"at index: %d",index);
break;
case 3:
NSLog(#"at index: %d",index);
break;
case 4:
NSLog(#"at index: %d",index);
break;
case 5:
NSLog(#"at index: %d",index);
break;
} }
- (void)viewDidLoad
{
aCarousel.type = iCarouselTypeInvertedRotary;
aCarousel.delegate = self;
aCarousel.dataSource = self;
//aCarousel.frame = CGRectMake(0, 68, 320, 257);
[super viewDidLoad];
}
#pragma - mark iCarousel Delegate
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [self.menuItems count];
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
// limit the number of item views loaded concurrently (for performance)
return 4;
}
- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, 240.0f, 200.0f);
[button setImage:[UIImage imageNamed:[menuItems objectAtIndex:index]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
//button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
return button;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
return 0;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
// usually this should be slightly wider than the item views
return 240; //for 220 px image
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
return self.wrap;
}
- (void)carouselDidScroll:(iCarousel *)carousel
{
[label setText:[NSString stringWithFormat:#"%#", [descriptions objectAtIndex:carousel.currentItemIndex]]];
}
I am really fed up trying adjusting carousel for hours. Although I tried aspectfitratio, adjusting frame but don't know why output is wrong. Can please someone figure out where am I doing wrong? Thanks alot :(
Atlast I was able to solve issue by myself. Don't know why my project was behaving so odd, so I decided to use iCarousel in new Project. The mistakes I noted were as follows:
In nib file, when I was making class type to iCarousel for subview, it was showing something like "I Carousel" in objects list - I changed the name in the list manually to my IBOutlet iCarousel object as "aCarousel".
Secondly I wasn't properly assigning UIButton to UIView in -(UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view method. I am glad that I found "Button demo" in Examples folder. From there I copied my code and now everything is working awesome! :)
Copying Code here for anybody who wants to see:
- (UIView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton *button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [UIImage imageNamed:[menuItems objectAtIndex:index]];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
//[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
//button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchDown];
}
//set button label
//[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
//Use these commented lines if you are displaying some text on button
return button;
}

Using of NStimer to set image animation with default image

How to integrate two images where one act as default and another as animating image when app launches..The animating image should not come again even that view loads again..it should come only when app launches like default.png image..The idea is to get two default images..How can i do that?..
Here is my code...
#interface ViewController ()<UIActionSheetDelegate>
#property (nonatomic, assign) BOOL useButtons;
#end
#implementation ViewController
#synthesize carousel,Chaintop;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
carousel.type = iCarouselTypeInvertedWheel ;
CGRect ChaintopFrame = Chaintop.frame;
ChaintopFrame.origin.y = self.view.bounds.size.height;
[UIView animateWithDuration:3
delay:1.0
options: UIViewAnimationCurveEaseOut
animations:^{
Chaintop.frame = ChaintopFrame;
//Chainbottom.frame = ChainbottomFrame;
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
[self.view addSubview: Chaintop];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return NUMBER_OF_ITEMS;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIImage *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:#"Cover_0.png"],
[UIImage imageNamed:#"Cover_1.png"],
[UIImage imageNamed:#"Cover_2.png"],
[UIImage imageNamed:#"Cover_3.png"],
[UIImage imageNamed:#"Cover_4.png"],
[UIImage imageNamed:#"Cover_5.png"],
[UIImage imageNamed:#"Cover_6.png"],
[UIImage imageNamed:#"Cover_7.png"],nil];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setImage:(UIImage*)[buttonImage objectAtIndex:index] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
-(void)dealloc{
[Chaintop release];
}
- (BOOL)carousel:(iCarousel *)_carousel shouldSelectItemAtIndex:(NSInteger)index
{
if (index == carousel.currentItemIndex)
{
NSLog(#"Should select current item");
}
else
{
NSLog(#"Should select item number %i", index);
}
return YES;
}
- (void)carousel:(iCarousel *)_carousel didSelectItemAtIndex:(NSInteger)index
{
if (index == carousel.currentItemIndex)
{
//note, this will only ever happen if useButtons == NO
//otherwise the button intercepts the tap event
NSLog(#"Did select current item");
}
else
{
NSLog(#"Did select item number %i", index);
}
}
#end
In your APP DELEGATE.
Something like this should do the job:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:self.window.bounds] autorelease];
UIImage *image = [UIImage imageNamed:#"NAMEOFYOURSPLASHSCREEN.png"];
imageView.image = image;
[self.window addSubview:imageView];
[self.window makeKeyAndVisible];
[self performSelector:#selector(remove1stSplash:) withObject:imageView afterDelay:5];
return YES;
}
- (void)remove1stSplash:(UIImageView *)1stView {
UIImageView *imageView = ...
[self.window addSubview:imageView];
[self performSelector:#selector(remove2ndSplash:) withObject:imageView afterDelay:5];
[1stView removeFromSuperView];
}
- (void)remove2ndSplash:(UIImageView *)2ndView {
[self.window addSubview:.....
[2ndView removeFromSuperView];
}
EDIT:
Link for a sample project:
Two Splash Screen display in iphone

Custom search results button has weird behaviour in UISearchBar

I subclassed UISearchBar in order to set custom image to search results button the following way (.m file):
#import "CustomUISearchBar.h"
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#implementation GipUISearchBar
- (void)layoutSubviews
{
UITextField *searchField = nil;
UIView *backGround = nil;
UIButton *cancelButton = nil;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++)
{
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
{
searchField = [self.subviews objectAtIndex:i];
}
else if ([[self.subviews objectAtIndex:i] isKindOfClass:NSClassFromString(#"UISearchBarBackground")])
{
backGround = [self.subviews objectAtIndex:i];
}
else if ([[self.subviews objectAtIndex:i] isKindOfClass:[UIButton class]])
{
cancelButton = [self.subviews objectAtIndex:i]; [cancelButton setTintColor:RGB(22.0,38.0,111.0)];
}
}
if(!(searchField == nil))
{
searchField.layer.cornerRadius=15.0f;
searchField.layer.masksToBounds=YES;
searchField.layer.borderColor = [RGB(26.0,141.0,223.0)CGColor];
searchField.layer.borderWidth = 2.0f;
UIImage *glassImage = [UIImage imageNamed: #"search_icon.png"];
UIImageView *iView = [[UIImageView alloc] initWithImage:glassImage];
searchField.leftView = iView;
[iView release];
UIImage *historyImage = [UIImage imageNamed:#"history_button.png"];
UIButton *historyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[historyButton setBackgroundImage:historyImage forState:UIControlStateNormal];
[historyButton setBackgroundImage:historyImage forState:UIControlStateSelected];
[historyButton setBackgroundImage:historyImage forState:UIControlStateDisabled];
[historyButton setBackgroundImage:historyImage forState:UIControlStateHighlighted];
historyButton.frame = CGRectMake(0,
0,
historyImage.size.width-3,
historyImage.size.height-3);
historyButton.layer.borderColor = [RGBA(0.0,0.0,0.0,0.0)CGColor];
historyButton.layer.borderWidth = 0.0f;
searchField.rightView = historyButton;
}
if (!(backGround == nil))
[backGround removeFromSuperview];
[super layoutSubviews];
}
The problem is when i touch inside search bar and it resizes i see this button being moved from left to right. How can i disable this behaviour?
try commenting below lines.
[super layoutSubviews];
Because it seems that you are overriding layout of UISearchBar by inheriting it.

UIButton failed to call functions?

I have a viewcontroller(mainViewController) and view class(UIView class, mainView). mainViewController, I called a method "generateView" from mainView class. Here is the code:
Method 1:
- (void) generateView {
container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
container.backgroundColor = [UIColor clearColor];
container.contentSize = CGSizeMake(500,1200);
container.showsVerticalScrollIndicator = YES;
[container setScrollEnabled:TRUE];
container.backgroundColor = [UIColor redColor];
int row = 0;
int col = 0;
for (int i = 0; i < 5; i++) {
if(i % 4 == 0 && i != 0){
row++;
col = 0;
}
UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:#"%#/images/items/%i.png", HOSTADDR, i ]]];
[b setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
[b setTag: i];
[b addTarget:self action:#selector(testFunction:) forControlEvents:UIControlEventTouchUpInside];
[b setEnabled:TRUE];
[b setUserInteractionEnabled:TRUE];
[container addSubview:b];
col++;
}
[self addSubview:container];
}
- (void) testFunction: (id) sender {
NSLog(#"Function called. Sender tag number: %i", [sender tag]);
}
Method 2:
- (void) generateView {
container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
container.backgroundColor = [UIColor clearColor];
container.contentSize = CGSizeMake(500,1200);
container.showsVerticalScrollIndicator = YES;
[container setScrollEnabled:TRUE];
container.backgroundColor = [UIColor redColor];
int row = 0;
int col = 0;
for (int i = 0; i < 5; i++) {
if(i % 4 == 0 && i != 0){
row++;
col = 0;
}
UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:#"%#/images/items/%i.png", HOSTADDR, i ]]];
[b setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
[b setTag: i];
[b addTarget:mainViewController action:#selector(updateData:) forControlEvents:UIControlEventTouchUpInside];
[b setEnabled:TRUE];
[b setUserInteractionEnabled:TRUE];
[container addSubview:b];
col++;
}
[self addSubview:container];
}
What I really need is method 2, cuz I want to call the updateData in mainViewController, but no matter how i tried, the button is not calling the function thats supposed to be called. Any idea?
EDIT:
This is how I called the view class
- (void) loadContent: (id) sender {
[self removeSubmenus];
switch ([sender tag]) {
case 2001:
{
MainView *mainView = [[MainView alloc] initWithSubmenu:CGRectMake(420, 85, 0, 0)];
[self.view addSubview:mainView];
break;
}
default:
break;
}
}
You should set delaysContentTouches to YES on the UIScrollView.
You're also leaking a bunch of memory. Make sure you're releasing both the UIButton (b) and NSData (imageData) objects as soon as you're done with them.
--
EDIT: I tried your code and it works fine. But it seems you wanted all this code in a UIView, so I threw together a quick sample.
TestView.h
#import <UIKit/UIKit.h>
#interface TestView : UIView {}
#end
TestView.m
#import "TestView.h"
#implementation TestView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIScrollView *container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
int row = 0;
int col = 0;
for (int i = 0; i < 5; i++) {
if(i % 4 == 0 && i != 0){
row++;
col = 0;
}
UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];
[b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
[b setTag: i];
[b addTarget:self action:#selector(testFunction:) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:b];
[b release];
col++;
}
[self addSubview:container];
}
return self;
}
- (void) testFunction: (id) sender {
NSLog(#"Function called. Sender tag number: %i", [sender tag]);
}
- (void)dealloc {
[super dealloc];
}
#end
TestController.h
#interface TestController : UIViewController { }
#end
TestController.m:
#import "TestController.h"
#import "TestView.h"
#implementation TestController
- (void) generateView {
TestView *tv = [[TestView alloc]initWithFrame:[self.view frame]];
[self.view addSubview:tv];
[tv release];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self generateView];
}
- (void)dealloc {
[super dealloc];
}
#end
Try adding this code to your method
container.delaysContentTouches = NO;
container.canCancelContentTouches = NO;
Pls modify your code to avoid memory leaks. Make sure you delete objects that you take ownership of.
Try this,
[b addTarget:mainViewController.view action:#selector(updateData:) forControlEvents:UIControlEventTouchUpInside];