1

I have:

  1. UIView
  2. UIViewController

In the UIViewController I need to be able to insert items into a fixed array of 6 integers. Then, I need to pass this array to the view in order for it to analyse the array and update the screen appropriately. How do I go about doing this? I've tried using standard C arrays but doesn't seem to work?

0

3 Answers 3

2

You can't use C arrays as arguments to methods, nor can you pass them to something. You have two options:

  1. Use an NSArray of six NSNumbers, each containing an int.
  2. Use a pointer to the C array.

Which one you choose is up to you. Option 1 is more iPhone-alike. Option 2 is less memory-consuming.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. Option 1 uses more memory, however it's quick and dirty way of getting things done.
1

Because objective c doesn't allow C arrays to be passed to objective c methods unless they are sent as pointers, Here is one method:

// MyViewController.m
#define BOX_INT(ARG_1) [NSNumber numberWithInt:ARG_1];

....

-(void) somethingHappened:(id) sender
{
    // initialize the array:
    NSArray *array = [NSArray arrayWithObjects:BOX_INT(ints[0]), BOX_INT(ints[1]), BOX_INT(ints[2]), BOX_INT(ints[3]), BOX_INT(ints[4]), BOX_INT(ints[5]), nil];
    [myView passArray:array];
}


// MyView.m
#define UNBOX_INT(ARG_1) [ARG_1 intValue];

....

-(void) passArray:(NSArray *) array
{
     int intArray[6];

     for (int i = 0; i < 6; i++)
     {
          intArray[i] = UNBOX_INT([array objectAtIndex:i]);
     }

     ...
}

Or, you can just pass a pointer (not recommended)

// MyController.m

-(void) somethingHappened:(id) sender
{
    [myView passArray:ints];
}

-(void) passArray:(int *) array
{
    // think of array as an integer array, with a length of 6.
}

6 Comments

May I ask what #define UNBOX_INT(ARG_1) [ARG_1 intValue]; does?
it defines a macro that is equivalent to the function: int UNBOX_INT(NSNumber *num) { return [num intValue]; }
Is it possible to declare this array in the header file, then access within the .m file individual elements to update? i.e. myArray[5] = 356;?
Yes, but it has to be a variable not a property. Variables are accessed by the pointer syntax.. e.g. myView->ints[i] = ints[i];
I see, so for example, if I have an int a = 5; and i want to insert that at position 4 of NSArray, what would that be?
|
0

There' has always been a discussion for how to share objects between two classes . So broadly speaking there are three methods:-

  1. Singleton Objects
  2. Global Variables
  3. Appdelegate variables

However if u really want to pass an array from ViewController to View , I would recommend going with 3rd method i.e. AppDelegate Variables. And yes that's always pointed that this is quick but Dirty method.

So what you can do is declare and Array in Appdelegate.

Now to insert items into that array in ur viewcontroller

UrAppDelegate* delegate = (UrAppDelegate*)[[UIApplication sharedApplication]delegate];
[delegate.urArrayName addObject:@"someObject"];

And then when u want to use that in ur view.. just go ahead and use the same method above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.